3

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

Community
  • 1
  • 1
NRJ
  • 1,064
  • 3
  • 15
  • 32

4 Answers4

3

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.

Isaac
  • 16,458
  • 5
  • 57
  • 81
  • When I stop the application, I get this message - Application admin-cob-ear on server server1 and node ppp-14415Node02 stopped successfully. Where do I make change to make it run on Node02? I did try deploying again but I did not see any option to change Node number. – NRJ May 27 '14 at 16:01
  • As part of the deployment process, you specify the "target mapping" - that is, which module (within the EAR) should run on which server(s) (or clusters). If you are deploying through the WAS administration UI, you can't possibly miss that dialogue. – Isaac May 27 '14 at 16:13
  • Right.I did map it correctly, so I figured that something is wrong with my installation of WAS. So, I have setup a new instance, now trying to run the app from new Instance – NRJ May 27 '14 at 17:08
0

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

Robert Panzer
  • 1,419
  • 12
  • 14
0

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>
trikelef
  • 2,192
  • 1
  • 22
  • 39
  • getDeploymentDescriptor An error occurred while processing the deployment descriptor, error message=Parent Translator (GenericTranslator(resource-ref,842215987)) did not find a Child Translator for "lookup-name". – NRJ May 27 '14 at 13:49
-1

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

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

This will probably fix the error

groo
  • 4,213
  • 6
  • 45
  • 69
  • Changing to this throws javax.naming.NameNotFoundException: Context: ppp-14415Node01Cell/nodes/ppp-14415Node01/servers/server1, name: jdbc/dbcob: First component in name dbcob not found. – NRJ May 23 '14 at 15:26
  • This is bad advice: applications should always use an indirect reference via java:comp/env, or else they'll be tightly coupled to the environment. – Brett Kail May 23 '14 at 16:22
  • @Raj - Have you created the DataSource in WebShere? Can you share it's configuration? – groo May 23 '14 at 17:21
  • @bkail - Well, I definitely respect your opinion. I on the other hand am the kind of guy that prefers real working applications than conceptual conventions that, sometimes, are not working or become more complex to use. But you're right, if the DS is defined and the mappings are correct the java:comp/env should work. Thing is that I've seen many WebSphere and WebLogic implementations where developers/admins just ignored the proper mappings for simplicity and than didn's use the namespace and it works. – groo May 23 '14 at 17:45
  • @MarcosMaia - Yes I have Datasource in Websphere. I have also pinged it using the Test Connection button. I checked C:\Program Files\IBM\WebSphere\AppServer1\profiles\AppSrv01\config\cells\ppp-14415Node01Cell\resources.xml, but I an not able to locate a JDBC resource named admincob. But this JDBC resource is visible in the Administration GUI, is there some other file which stores JDBC configuration ? – NRJ May 23 '14 at 18:25
  • @Raj - That's strange. I would try dumping the namespace next. Also double check the scope you have created your DataSource in WAS. http://www-01.ibm.com/support/knowledgecenter/api/content/SS7K4U_8.5.5/com.ibm.websphere.nd.multiplatform.doc/ae/rnam_dump_utility.html – groo May 23 '14 at 18:39