8

I have created a JNDI data-source using my weblogic console but I am not able to access the object from my web application. Below are the details

In weblogic 10.3.6, I have given the JNDI name for datasource as : jdbc/mydb

To get DB connection from my web application I have written this code in my web application:

Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
jndiConnection = ds.getConnection();

Earlier I was using Tomcat as server and I was able to get DB connection when I configured the resource details in the file tomcat/conf/server.xml, but when I am using started using weblogic server I am getting below error:

Cannot establish DB connection to JNDI:java:/comp/env/jdbc/mydb While trying to look up /comp/env/jdbc/mydb in /app/webapp/sample.war/1811641702. caused by: javax.naming.NameNotFoundException: While trying to look up /comp/env/jdbc/mydb in /app/webapp/sample.war/1811641702.; remaining name '/comp/env/jdbc/mydb'

I have tried the options mentioned in this link : How to lookup JNDI resources on WebLogic? but still I am facing problems.

Please let me know where I am doing mistake, what is the process of accessing the JNDI object.

Community
  • 1
  • 1
chaitanya
  • 701
  • 4
  • 14
  • 26
  • 1
    Did you mean to name it "jdbc/mydb" instead of "java/mydb"? – Display Name is missing Apr 25 '13 at 17:22
  • 2
    Also have you tried the second solution in the link in your question? The first answer applies to Tomcat but I'm not sure that always works in weblogic. Remove java:comp/env/ and try initContext.lookup("jdbc/mydb")? – Display Name is missing Apr 25 '13 at 18:34
  • yes, even that is not working, I am getting below error in that case: Cannot establish DB connection to JNDI:jdbc/mydb While trying to lookup 'jdbc.mydb' didn't find subcontext 'jdbc'. Resolved '' caused by: javax.naming.NameNotFoundException: While trying to lookup 'jdbc.mydb' didn't find subcontext 'jdbc'. Resolved ''; remaining name 'jdbc/mydb' – chaitanya Apr 25 '13 at 18:46
  • The answer works on Weblogic 12.1.3 as well – rjdkolb Sep 22 '15 at 12:38

3 Answers3

10

After referring to the post:Tomcat vs Weblogic JNDI Lookup I have modified my code.

Using below code in my java program of web application has solved my issue:

Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("jdbc/mydb");
jndiConnection = ds.getConnection();

Also in weblogic console I have added my JNDI object to my Admin Server (under servers option) where my web application is deployed.

Community
  • 1
  • 1
chaitanya
  • 701
  • 4
  • 14
  • 26
3

Tried your answer in weblogic 12c but not worked..!

When i tried by using only the name of dataSource myDB (removed the jdbc/) it worked fine.

Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("myDB");
jndiConnection = ds.getConnection();
vardhan
  • 1,377
  • 11
  • 14
2

The same solution for Weblogic 12c would be

add the below dependency to your pom.xml.. create a variable with current middleware home value ${oracleMiddlewareHome}, then...

<dependency>
        <groupId>weblogic</groupId>
        <artifactId>webservices</artifactId>
        <version>12.1.3</version>
        <scope>system</scope>
        <systemPath>
            ${oracleMiddlewareHome}/wlserver/server/lib/weblogic.jar
        </systemPath>
    </dependency>

Now use the below code :

 Hashtable<String, String> h = new Hashtable<String, String>(7);
h.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
h.put(Context.PROVIDER_URL, "t3://localhost:7001");//add ur url
h.put(Context.SECURITY_PRINCIPAL, "weblogic");//add username
h.put(Context.SECURITY_CREDENTIALS, "welcome1");//add password

    Bundle bundle;
    try {
        InitialContext ctx = new InitialContext(h);
       DataSource dataSource = ((DataSource) ctx.lookup("jdbc/ContextBindingDS"));
        bundle = (Bundle) ctx.lookup(BUNDLE_JNDI_NAME);


    } catch (NamingException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }catch (Exception e){
        e.printStackTrace();
    }
spattanaik75
  • 143
  • 9