How to lookup JNDI resources on WebLogic?
Asked Answered
T

4

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!

Tambourin answered 28/6, 2011 at 1:18 Comment(0)
I
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

I answered 28/6, 2011 at 22:50 Comment(3)
Thanks Jeff, is this with the assumption that my data source jndi name is prefixed with "jdbc/" in weblogic?Tambourin
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 - [email protected] :)I
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.I
A
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.

Appreciable answered 28/6, 2011 at 3:2 Comment(8)
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.Tambourin
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?Appreciable
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.Tambourin
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?Appreciable
thanks I'll try that. It was originally deployed on JBoss with datasource xml files.Tambourin
you do not need the java: prefixI
rather, you don't need the "java:comp/env/" prefix.I
Yes, I had similar issue in WebLogic and removing the prefix worked. dataSource = (javax.sql.DataSource)context().lookup("MyDatasource");Joshi
Y
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.

Yerkovich answered 12/3, 2013 at 23:30 Comment(0)
T
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.

Tichonn answered 9/10, 2013 at 14:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.