Search code examples
javajakarta-eewebspherejndiwebsphere-7

JNDI - JDBC resource lookup failes in WAS 7


My code is unable to do lookup a JDBC resource using JNDI. I am getting the following exception:

[Root exception is javax.naming.NameNotFoundException: Context: ppp-14415Node01Cell/nodes/ppp-14415Node01/servers/server1, name: jdbc/admincob: First component in name admincob not found. [Root exception is org.omg.CosNaming.NamingContextPackage.NotFound: IDL:omg.org/CosNaming/NamingContext/NotFound:1.0]]

I have followed these 2 solution on SO, but still its not working

Websphere 6.1 to 7 how to update ibm-web-bnd.xmi to ibm-web-bnd.xml

How do I connect to a Websphere Datasource with a given JNDI name?

Here is my ibm-web-bnd.xml

    <virtual-host name="default_host" />
<resource-ref name="jdbc/dbcob" binding-name="jdbc/admincob" />

and portion of web.xml

    <resource-ref>
    <description>
Datasource connection to db</description>
    <res-ref-name>jdbc/dbcob</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    <res-sharing-scope>Shareable</res-sharing-scope>
</resource-ref>

Here is the screenshot of the binding: binding description

Lookup code:

       Context initialContext = new InitialContext();

        DataSource datasource = (DataSource) initialContext
                .lookup("java:comp/env/jdbc/dbcob");
        if (datasource != null) {
            result = datasource.getConnection();
            System.out.println("Data connection retrieved");
            result.close();
        } else {
            System.err.println("Failed to lookup datasource.");
        }

I am not sure what am I missing. Please help.

enter image description here enter image description here


Solution

  • This happens because your data source is defined within the scope Cell:/Node:14415Node02/Server:server1 (as per your screenshot of the Data Source definition), whereas your application is configured to run on Cell:/Node:14415Node01/Server:server1 (as per the exception text that you attached).

    In other words, you're running your application on server server1 on node A, whereas the Data Source definition is scoped to node B. This Data Source can only be seen by servers that are scoped within node B.