11

I deployed a legacy application on WebLogic 11g. The application has the following code:

 Context context = new InitialContext();
 dataSource = (javax.sql.DataSource) context.lookup("java:myDataSource");

I also have a data source configured in WebLogic with the JNDI name of:

     jdbc/myDataSource

When the above java code runs, I get the following exception:

       javax.naming.NameNotFoundException: While trying to look up /myDataSource in /app/webapp/axis2.war/60105275.; remaining name '/myDataSource'
        at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)

      at weblogic.jndi.internal.ApplicationNamingNode.lookup(ApplicationNamingNode.java:144)

I'm fairly new to JNDI, so my question is? Where is the disconnect in naming? What does it mean when a context lookup has a prefix of "java:" ?

Thanks!

wsb3383
  • 3,841
  • 12
  • 44
  • 59

4 Answers4

8

You should be able to simply do this:

Context context = new InitialContext();
dataSource = (javax.sql.DataSource) context.lookup("jdbc/myDataSource");

If you are looking it up from a remote destination you need to use the WL initial context factory like this:

Hashtable<String, String> h = new Hashtable<String, String>(7);
h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, pURL); //For example "t3://127.0.0.1:7001"
h.put(Context.SECURITY_PRINCIPAL, pUsername);
h.put(Context.SECURITY_CREDENTIALS, pPassword);

InitialContext context = new InitialContext(h);
dataSource = (javax.sql.DataSource) context.lookup("jdbc/myDataSource");

weblogic.jndi.WLInitialContextFactory

Mahdi Esmaeili
  • 555
  • 7
  • 11
Jeff West
  • 1,563
  • 9
  • 11
  • Thanks Jeff, is this with the assumption that my data source jndi name is prefixed with "jdbc/" in weblogic? – wsb3383 Jun 28 '11 at 22:59
  • No. You mention in your post that the JNDI name is "jdbc/myDataSource". If the JNDI name is "I.Am.Jeff.West" then you would do dataSource = (javax.sql.DataSource) context.lookup("I.Am.Jeff.West"). However, I personally like to organize things with a prefix that makes the JNDI tree cleaner. By the way, you never responded to my request to ask about you migrating from JBoss to WebLogic - jeffrey.west@oracle.com :) – Jeff West Jun 28 '11 at 23:55
  • Actually, I suppose the answer to your question could be 'yes'. Either way, when you use the WebLogic Initial Context Factory you specify the full JNDI name with no other prefix. – Jeff West Jun 30 '11 at 22:03
6

java is the root JNDI namespace for resources. What the original snippet of code means is that the container the application was initially deployed in did not apply any additional namespaces to the JNDI context you retrieved (as an example, Tomcat automatically adds all resources to the namespace comp/env, so you would have to do dataSource = (javax.sql.DataSource) context.lookup("java:comp/env/jdbc/myDataSource"); if the resource reference name is jdbc/myDataSource).

To avoid having to change your legacy code I think if you register the datasource with the name myDataSource (remove the jdbc/) you should be fine. Let me know if that works.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • Thanks for the answer. I tried doing that, but i'm still getting the same error. I'm not sure if there's some configuration issue with the WebLogic server itself. – wsb3383 Jun 28 '11 at 17:15
  • Can you show your WebLogic resource definition (minus passwords/hostnames and such)? Also, I'm assuming you can't (or would rather not) recompile the code? – Femi Jun 28 '11 at 17:37
  • Are you talking about the weblogic.xml? I don't have any. I only have Web-based admin console access to the server. I defined a generic data source and entered myDataSource for a JNDI name. – wsb3383 Jun 28 '11 at 18:01
  • Ah, interesting: you might be able to add a resource ref for the specific legacy application that would correct the error, so I'd play with that interface. However you may still need to recompile your source if you can't quite get it to work. What J2EE server was the application deployed on originally? – Femi Jun 28 '11 at 19:50
  • thanks I'll try that. It was originally deployed on JBoss with datasource xml files. – wsb3383 Jun 28 '11 at 21:16
  • you do not need the java: prefix – Jeff West Jun 28 '11 at 22:51
  • rather, you don't need the "java:comp/env/" prefix. – Jeff West Jun 29 '11 at 06:20
  • Yes, I had similar issue in WebLogic and removing the prefix worked. dataSource = (javax.sql.DataSource)context().lookup("MyDatasource"); – iampranabroy May 14 '19 at 07:27
1

I had a similar problem to this one. It got solved by deleting the java:comp/env/ prefix and using jdbc/myDataSource in the context lookup. Just as someone pointed out in the comments.

Uwe Günther
  • 2,971
  • 2
  • 21
  • 26
1

I just had to update legacy Weblogic 8 app to use a data-source instead of hard-coded JDBC string. Datasource JNDI name on the configuration tab in the Weblogic admin showed: "weblogic.jdbc.ESdatasource", below are two ways that worked:

      Context ctx = new InitialContext();
      DataSource dataSource;

      try {
        dataSource = (DataSource) ctx.lookup("weblogic.jdbc.ESdatasource");
        response.getWriter().println("A " +dataSource);
      }catch(Exception e) {
        response.getWriter().println("A " + e.getMessage() + e.getCause());
      }

      //or

      try {
        dataSource = (DataSource) ctx.lookup("weblogic/jdbc/ESdatasource");
        response.getWriter().println("F "+dataSource);
      }catch(Exception e) {
        response.getWriter().println("F " + e.getMessage() + e.getCause());
      }

      //use your datasource
      conn = datasource.getConnection();

That's all folks. No passwords and initial context factory needed from the inside of Weblogic app.

wholenewstrain
  • 199
  • 1
  • 8