2
votes

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 hereenter image description here

4
None of your screenshots prove that there actually is a Data Source bound to the name jdbc/admincob. - Isaac
On WAS 8, the lookup only worked without "java:comp/env/" for me. - Martin Baumgartner
I have tried using initialContext .lookup("jdbc/dbcob"); It still does not work. - NRJ
@Isaac - Updated the screenshot of datasource - NRJ

4 Answers

3
votes

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.

0
votes

Your binding seems to be ok. java:comp/env/jdbc/dab cob is correctly mapped to jdbc/dbcob. The SystemOut.log of the server should show if a DataSource is bound and under which name.

Kind regards Robert

0
votes

Try something like that in your web.xml descriptor:

<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>
    <lookup-name>jdbc/admincob</lookup-name>
</resource-ref>
-1
votes

Martin Baumgartner is probably right, try changing your lookup to:

DataSource datasource = (DataSource) initialContext
            .lookup("jdbc/dbcob");

This will probably fix the error