|
Problem(Abstract) |
An application running in WebSphere® Application V5 or V6
receives the following exception when doing a Java™ Naming and Directory
Interface (JNDI) lookup of a resource:
"javax.naming.NamingException - Name comp/env/jdbc not found in context
"java:" |
|
|
|
Cause |
The most likely cause of this exception is that the
application does an indirect JNDI lookup, but a resource reference does
not exist for the resource that is looked up. To provide some background
information, there are two different types of JNDI lookups in WebSphere
Application Server:
- A direct, or global, JNDI lookup occurs when the application
looks up the physical JNDI name of a resource that is bound to the
namespace. For example, if the JNDI name of a data source is "jdbc/myDS",
the lookup would look like this:
DataSource ds = (DataSource)ctx.lookup("jdbc/myDS");
- An indirect, or local, JNDI lookup occurs when the application
looks up a resource reference which maps to the physical JNDI name. A
resource reference is defined in the application deployment descriptor,
web.xml for a Web module, ejb-jar.xml for an EJB module, or
application-client.xml for an application client module. For example, if
the resource reference name is "jdbc/myDSresref", the lookup would look
like this:
DataSource ds =
(DataSource)ctx.lookup("java:comp/env/jdbc/myDSresref");
Indirect JNDI lookups are considered a J2EE best practice. The advantage
of an indirect JNDI lookup is that it makes your applications easier to
maintain. If the actual JNDI name of a resource changes, no changes need
to be made to your application. This is because the JNDI name of the
resource is stored in the resource reference, not in the application code.
If a resource reference does not exist in the application module, the JNDI
lookup will fail with the javax.naming.NamingException mentioned
above. It is also important to note that an indirect JNDI lookup can only
be done from an application within a J2EE container (Web module, EJB
module, or application client module). |
|
|
Resolving the
problem |
To resolve the problem, you'll need to create a resource
reference in the deployment descriptor for your Web, EJB, or application
client module, and map it to the physical JNDI name for your resource.
This can be done using the Application Server Toolkit (AST), WebSphere
Studio Application Developer (WSAD), or Rational Application Developer
(RAD).
The name of the resource reference is the part of the JNDI name that comes
after "java:comp/env" when the indirect JNDI lookup is done. When
configuring the resource reference, the physical JNDI name of the resource
is set as the JNDI Binding. |
|
|
|
|
Cross Reference information |
Segment |
Product |
Component |
Platform |
Version |
Edition |
Application Servers |
Runtimes for Java Technology |
Java SDK |
|
|
|
|
|
|