Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to lookup JNDI resources on WebLogic?

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!

like image 232
wsb3383 Avatar asked Jun 28 '11 01:06

wsb3383


People also ask

Where is JNDI in WebLogic?

The JNDI binding for an object can appear in the JNDI tree for one WebLogic Server in the cluster, or it can be replicated to all the WebLogic Servers in the cluster. If the object of interest is bound in only one WebLogic Server, you must explicitly connect to the host WebLogic Server by setting the Context.

How do I look up a JNDI?

To obtain a reference to a data source bound to the JNDI context, look up the data source's JNDI name from the initial context object. The object retrieved in this way is cast as a DataSource type object: ds = (DataSource)ctx. lookup(JndiDataSourceName);

What is JNDI name in WebLogic?

JNDI provides a common-denominator interface to many existing naming services, such as LDAP (Lightweight Directory Access Protocol) and DNS (Domain Name System). These naming services maintain a set of bindings, which relate names to objects and provide the ability to look up objects by name.


1 Answers

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

like image 184
Jeff West Avatar answered Oct 12 '22 23:10

Jeff West