4 Replies Latest reply: May 9, 2013 3:15 AM by Rock Ching RSS

    How to access JNDI data source defined in weblogic 10.3.6

    kumar_1302
      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 : java/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'

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

      Edited by: kumar_1302 on Apr 25, 2013 8:57 AM
        • 1. Re: How to access JNDI data source defined in weblogic 10.3.6
          Vijaya Moderator -Oracle
          Kumar,

          Please change the below line of code from
          DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/mydb");
          to

          DataSource ds = (DataSource)initContext.lookup("java:comp/env/jdbc/mydb");

          Let us know if the issue still persists...

          Thanks,
          Vijaya
          • 2. Re: How to access JNDI data source defined in weblogic 10.3.6
            kumar_1302
            Thanks a lot Vijaya for replying.

            I tried your suggesting but still 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/92552029. caused by: javax.naming.NameNotFoundException: While trying to look up comp/env/jdbc/mydb in /app/webapp/sample.war/92552029.; remaining name 'comp/env/jdbc/mydb'

            As per error message it says that it is trying to get the JNDI object from my sample web application:

            javax.naming.NameNotFoundException: While trying to look up comp/env/jdbc/mydb in /app/webapp/sample.war/92552029.

            But I have created it in my weblogic console, how can I map that to my web-application sample.war, is there any additional step that I am missing to configure the JNDI object?
            • 3. Re: How to access JNDI data source defined in weblogic 10.3.6
              Vijaya Moderator -Oracle
              Well.. you can check the JNDI Tree in the weblogic Server Admin console -Servers Section to see if the mydb object is binded to the tree.

              If it is not binded to the tree, it is obvious that you end up with NameNotFoundException thrown by the JNDI subsystem.

              You will also need to ensure that the mydb datasource is created in the weblogic server successfully.

              Please the datasource configuration file that exists under domain_home\config\jdbc directory.

              Hope this helps!!

              Thanks,
              Vijaya
              • 4. Re: How to access JNDI data source defined in weblogic 10.3.6
                Rock Ching
                Hi Kumar,

                I believe you can simply use "jdbc/mydb" to lookup the datasource. Or if you want to use the compatible naming, you need to specify resource mapping in web.xml.

                Regards,
                Rock (Oracle ACS)