How to access JNDI data source defined in weblogic 10.3.6

谁说胖子不能爱 提交于 2019-12-01 17:07:27
chaitanya

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.

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();

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();
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!