Skip to main content

Can't find my DataSource with InitialContext via JNDI

2 replies [Last post]
Henrik_T
Offline
Joined: 2013-03-29
Points: 0

Hello!

I have wrestled with this issue for a few days, now. I have searched the internet, but I can't find any solution.

I use GlassFish 3.1.2.2 and I have set up a new DataSource and Resource for a Derby database. The database works fine. That is, it is possible to ping it from GlassFish. However, when I try to get the DataSource from code, it can't find it.

I can list the JNDI entries from the command line (the __FoodNutritionDB is displayed)
$ asadmin list-jndi-entries --context jdbc
__default: org.glassfish.javaee.services.ResourceProxy
__FoodNutritionDB: org.glassfish.javaee.services.ResourceProxy
__TimerPool: org.glassfish.javaee.services.ResourceProxy
Command list-jndi-entries executed successfully.

but, when I try to get the DataSource from a simple Servlet:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
   response.setContentType("text/html");
   PrintWriter writer = response.getWriter();
   try {
      InitialContext context = new InitialContext();
      Object obj = context.lookup("jdbc/__FoodNutritionDB");
      writer.println(obj.getClass().toString());
   } catch (NamingException e) {
      writer.println("Exception: " + e.toString() );
   }
   finally {
      writer.close();
   }
}

.. it fail with an exception: Exception: javax.naming.NamingException: Lookup failed for 'jdbc/__FoodNutritionDB' in SerialContext [Root exception is javax.naming.NameNotFoundException: __FoodNutritionDB not found]

The JNDI names jdbc/__default and jdbc/__TimerPool are found with the above code.

I have created the data source via GlassFish Web Console. But, a look at the domain.xml (a part of it), is probably helpful in debugging this issue. See the attached picture.

If anyone could help me with this one, I would be so grateful! Thanks!

AttachmentSize
JNDI-problem.png149.16 KB

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Henrik_T
Offline
Joined: 2013-03-29
Points: 0

I have made a few more experiments and discovered that it is Eclipse and the deployment inside Eclipse that is the villain. If I deploy the war-file manually into GlassFish autodeploy folder everything works. Have anyone an idea why JNDI lookup fails in Eclipse deploy? What is the prefered way of deploy? Maven?

lemon_zhang
Offline
Joined: 2013-01-03
Points: 0

I have tried the deployment from eclipse ( Juno + Glasfish Tools for Juno )
It works fine when look up FoodNutritionDB ..
Please check with the configuration of the Servers settings in your eclipse.