Skip to main content

Can't find my DataSource with InitialContext via JNDI

Please note these forums are being decommissioned and use the new and improved forums at
2 replies [Last post]
Joined: 2013-03-29


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 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
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 {
   PrintWriter writer = response.getWriter();
   try {
      InitialContext context = new InitialContext();
      Object obj = context.lookup("jdbc/__FoodNutritionDB");
   } catch (NamingException e) {
      writer.println("Exception: " + e.toString() );
   finally {

.. 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!

JNDI-problem.png149.16 KB

Reply viewing options

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

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?

Joined: 2013-01-03

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.