6/30/2006

Fix NameNotFoundException: Incorrect Lookup Name

javax.naming.NameNotFoundException is the most thrown exception I've seen in J2EE and JavaEE applications. In this series, I will explain some common causes and how to solve it.

Incorrect lookup names can take many forms. One of them is missing the standard prefix java:comp/env/when looking up resources or EJB in components' environment.

public class HelloServlet extends HttpServlet {
protected void processRequest(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = null;
try {
out = response.getWriter();
Context ic = new InitialContext();

//this line is wrong!
DataSource dataSource = (DataSource) ic.lookup("jdbc/default-datasource");
Connection connection = dataSource.getConnection();
out.println("Successfully looked up the default datasource: " + dataSource +
", and got the connection: " + connection);
} catch (NamingException e) {
throw new ServletException(e);
} catch (SQLException e) {
throw new ServletException(e);
}
}

protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
And the web.xml is as follows:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.foo.servlet.HelloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>
<resource-ref>
<res-ref-name>jdbc/default-datasource</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
<res-sharing-scope>Shareable</res-sharing-scope>
<mapped-name>jdbc/__default</mapped-name>
</resource-ref>
</web-app>
mapped-name subelement maps the local resource reference name jdbc/default-datasource used in Servlet code to the global JNDI name jdbc/__default configured in application server. mapped-name is a new way for mapping resource and EJB references in Java EE 5. jdbc/__default is the default datasource pre-configured in JavaEE SDK 5, Glassfish, and Sun Java System Application Server. Nothing is wrong with this configuration.

But when I deploy and run the above web app to JavaEE SDK 5, I get the following error:
javax.naming.NameNotFoundException
at com.sun.enterprise.naming.TransientContext.resolveContext(TransientContext.java:255)
at com.sun.enterprise.naming.TransientContext.lookup(TransientContext.java:178)
at com.sun.enterprise.naming.SerialContextProviderImpl.lookup(SerialContextProviderImpl.java:61)
at com.sun.enterprise.naming.LocalSerialContextProviderImpl.lookup(LocalSerialContextProviderImpl.java:98)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:309)
at com.sun.enterprise.naming.NamingManagerImpl.lookup(NamingManagerImpl.java:885)
at com.sun.enterprise.naming.java.javaURLContext.lookup(javaURLContext.java:156)
at com.sun.enterprise.naming.SerialContext.lookup(SerialContext.java:307)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.foo.servlet.HelloServlet.processRequest(HelloServlet.java:29)
The root cause is the wrong lookup name is used in HelloServlet. To fix this bug, just change the lookup code to use absolute name:
DataSource dataSource = (DataSource)
ic.lookup("java:comp/env/jdbc/default-datasource");
Alternatively, you can also lookup the subcontext first, and then lookup the short resource reference name:
Context subcontext =
(Context) ic.lookup("java:comp/env");
DataSource dataSource = (DataSource)
subcontext.lookup("jdbc/default-datasource");

All posts in this series for NameNotFoundException:

Fix NameNotFoundException: Incorrect Lookup Name

Fix NameNotFoundException: Reference Not Declared

Fix NameNotFoundException: Wrong Mapping in Deployment Plan

5 comments:

Anonymous said...

You can additionally browse/view the jndi tree on the app server to see under which 'name' your object is deployed.

http://tripoverit.blogspot.com/2007/03/print-jndi-tree

Anonymous said...

You save a lot of my time. Thanks!

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Red Ball 4 said...

Great article, I was very impressed about it, wish you would have stayed next share
ninjago game | swords and souls game | strikeforce kitty 2 | swords and souls 2 | strike force kitty | red ball 3

tinh linh said...

Thank you very much for your post, So kind for you, I also hope you will make more and more excellent post and let’s more and more talk, thank you very much, dear. Friv 5000 Games is abbreviation for free flash games. Our friv games are 100% safe to play, thank you.
Friv 5000