Search code examples
jakarta-eetomcat7jndiibm-midrange

javax.naming.NameNotFoundException: Name [jdbc/mydb] is not bound in this Context


I know this question has been asked several times. I found some links here and here as an example of what I found. But I try their solutions and my problem is not solved yet...

So I copy what I have:

  • A J2EE project in Eclipse. I built a new .war to install in an application server
  • I created a Tomcat 7 Server in Eclipse and added my .war to it to deploy it locally

So phisically in my project I have a .war project and the Servers project that auto-creates when you create a new Server in Eclipse.

I tried to connect to a local as400 db but when I did so, I found this error:

Error com.myprojectpackage.xxx: Name XXX is not bound in this Context

I tried to add a ResourceLink as said in one of the links I attach:

 <ResourceLink name="jdbc/mydb"
         global="jdbc/mydb"
         type="javax.sql.DataSource" />

First I tried to create it in my application like this:

  1. In my web.xml

    <resource-ref> <description>Datasource for my db</description> <res-ref-name>jdbc/mydb</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>

  2. I created a custom context.xml file and added

<ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="myuser" password="mypass" auth="Container" driverClassName="com.ibm.as400.access.AS400JDBCDriver" url="jdbc:as400://192.168.1.1;naming=system;errors=full;" />

This first try didn't work. So I went to the server side, and added the following:

  1. In context.xml file:

<ResourceLink name="jdbc/mydb" global="jdbc/mydb" type="javax.sql.DataSource" maxActive="50" maxIdle="30" maxWait="10000" username="myuser" password="mypass" auth="Container" driverClassName="com.ibm.as400.access.AS400JDBCDriver" url="jdbc:as400://192.168.1.1;naming=system;errors=full;" />

  1. In server.xml I added the same ResourceLink inside Context

This second try failed too.

So, what the problem is? How can I solve this?


Solution

  • Your first approach failed because you must provide a factory class for your DataSource, so that Tomcat be aware how to create connection, what driver to use and so on.

    The second one failed because of not properly used ResourceLink element.

    As stated in the Tomcat's documentation:

    This element is used to create a link to a global JNDI resource.

    Instead, you should create the following declaration into the META-INF/context.xml file:

    <Context>
        <Resource name="jdbc/mydb"
                  global="jdbc/mydb" 
                  type="javax.sql.DataSource" maxActive="50" maxIdle="30" 
                  maxWait="10000" 
                  username="myuser" password="mypass" auth="Container" 
                  driverClassName="com.ibm.as400.access.AS400JDBCDriver" 
                  url="jdbc:as400://192.168.1.1;naming=system;errors=full;"/>
    </Context>
    

    Then make sure the META-INF folder located at the proper place - you must include it within /webapp folder of your .war package.

    Now you can refer to your DataSource through JNDI by using the following name:

    "java:comp/env/jdbc/mydb"