Troubleshooting
Problem
An application running in WebSphere Application 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:
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).
- 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.
Creating or changing a resource reference https://www.ibm.com/docs/en/was/9.0.5?topic=applications-creating-changing-resource-reference |
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.
[{"Product":{"code":"SSEQTP","label":"WebSphere Application Server"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"JNDI\/Naming","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF010","label":"HP-UX"},{"code":"PF016","label":"Linux"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"}],"Version":"9.0;8.5.5;8.0;7.0","Edition":"Edition Independent","Line of Business":{"code":"LOB45","label":"Automation"}},{"Product":{"code":"SSNVBF","label":"Runtimes for Java Technology"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Component":"Java SDK","Platform":[{"code":"","label":""}],"Version":"","Edition":"","Line of Business":{"code":"LOB36","label":"IBM Automation"}}]
Was this topic helpful?
Document Information
Modified date:
22 June 2021
UID
swg21106933