Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebSphere: JNDI Context Lookup Failure

Tags:

websphere

I have the following...

Context aContext = = new InitialContext(settings);
aContext.lookup("java:comp/env/DB2_DB");

Also tried...

aContext.lookup("DB2_DB");

web.xml

<resource-ref>
    <description>
    </description>
    <res-ref-name>DB2_DB</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Application</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
    <mapped-name>DB2_DB</mapped-name>
</resource-ref>

then in my ibm-web-bnd.xml...

<resource-ref name="DB2_DB" binding-name="jdbc/DB2DB" />

In Websphere I see the binding name in resources>JDBC>Data Sources

But when I run my application I see...

Caused by: javax.naming.NameNotFoundException: Context: Node04Cell/nodes/Node04/servers/server1, name: DB2_DB: First component in name DB2_DB not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]^M

This is a port project from WAS6-8.5

like image 439
Jackie Avatar asked Apr 11 '13 15:04

Jackie


People also ask

Which exception do you use to detect if a lookup on a JNDI resource has failed?

If a resource reference does not exist in the application module, the JNDI lookup will fail with the javax. naming. NamingException mentioned above.

What is JNDI lookup in Java?

The Java Naming and Directory Interface (JNDI) is a Java API for a directory service that allows Java software clients to discover and look up data and resources (in the form of Java objects) via a name.

What is Java comp env in JNDI?

java:comp/env is the node in the JNDI tree where you can find properties for the current Java EE component (a webapp, or an EJB). Context envContext = (Context)initContext.lookup("java:comp/env");


1 Answers

Well, this question is quite old, and I see that there's no accepted answer yet, so.

Here is what really happens:

  1. Your code executes a JNDI lookup to java:comp/env/DB2_DB.
  2. WebSphere uses the WAS-proprietary deployment descriptor (ibm-web-bnd.xml) to "translate" the application binding DB2_DB into a real name in the WebSphere JNDI tree (jdbc/DB2DB).
  3. WebSphere looks up jdbc/DB2DB and returning it to the caller.

You are getting a NameNotFoundException on the first lookup - the lookup of java:comp/env/DB2_DB. The problem is not with finding jdbc/DB2DB; it's with finding DB2_DB inside your component's environment.

Your deployment descriptor looks OK to me, so I'm guessing that the reason for your problem is this:

Context aContext = new InitialContext(settings);

You are constructing an InitialContext instance by providing a Hashtable. The Hashtable is often useful when you need to provide special parameters for the construction, but you must know when to use it and when to avoid it. Code that runs inside a JavaEE container and needs simple access to the container's JNDI tree rarely, if ever, should provide any Hashtable to the InitialContext constructor.

I wouldn't be surprised if those settings that you're passing into InitialContext contain, for example, a PROVIDER_URL key instructing the lookup to happen on some distant foreign JNDI tree.

So, I would start by scrapping that parameter:

Context aContext = new InitialContext();

And then give it another shot.

If that still fails, use WebSphere's dumpNamespace utility to get a clear picture of WebSphere's JNDI tree.

like image 161
Isaac Avatar answered Oct 30 '22 04:10

Isaac