8 Replies Latest reply on Jun 7, 2010 11:25 AM by TakhteJamshid

    javax.naming.NameNotFoundException  exception when look up my Datasource

    TakhteJamshid
      Hello

      I'm new in J2EE and Weblogic

      I've installed Weblogic 10 and JDeveloper in my computer
      in Weblogic (Administration Console) I've created a DataSource that its JNDI Name is OracleConnection1 (in
      Summary of JDBC Data Sources in Data Sources table the value of JNDI Name for this datasource is OracleConnection1)

      and in JDeveloper I've create a client application that use this DataSource my code is like this :

      -----------------------------------------------------------------
      private static String testJDBC() throws Exception{
      Hashtable env = new Hashtable();
      // WebLogic Server 10.x connection details
      env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
      env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
      try{
      Context context=new InitialContext( env );
      javax.sql.DataSource ds=(javax.sql.DataSource) context.lookup ("OracleConnection1");
      return("ok");
      }catch(Exception ex){
      ex.printStackTrace();
      }

      return ("No");
      }
      -----------------------------------------------------------------

      but this line javax.sql.DataSource ds=(javax.sql.DataSource) context.lookup ("OracleConnection1")

      return this error :

      javax.naming.NameNotFoundException: Unable to resolve 'OracleConnection1'. Resolved '' [Root exception is javax.naming.NameNotFoundException: Unable to resolve 'OracleConnection1'. Resolved '']; remaining name 'OracleConnection1'
           at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:234)
           at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348)
           at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259)
           at weblogic.jndi.internal.ServerNamingNode_1032_WLStub.lookup(Unknown Source)
           at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:405)
           at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:393)
           at javax.naming.InitialContext.lookup(InitialContext.java:392)
           at oracle.HRFacadeClient.testJDBC(HRFacadeClient.java:71)
           at oracle.HRFacadeClient.main(HRFacadeClient.java:18)
      Caused by: javax.naming.NameNotFoundException: Unable to resolve 'OracleConnection1'. Resolved ''
           at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)
           at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:252)
           at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:182)
           at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:206)
           at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
           at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:589)
           at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
           at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:477)
           at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
           at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
           at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:473)
           at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
           at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
           at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)




      Do you know what is wrong ?

      Thank you
        • 1. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
          Jay SenSharma MiddlewareMagic
          Hi,

          Are u able to Test Connection of DataSource from AdminConsole?
          Is your DataSource targeted to a server which is running on 127.0.0.1:7001?

          Can u please run the following utility to Test Your Database connectivity is OK or not:

          Step1). Add JDBC Driver also in the Classpath or Better run "setWLSEnv.sh"

          Step2). Use WLS DB Ping utility:
          Syntax:
          java -classpath /bea11g/wl_server103/server/lib/weblogic.jar utils.dbping ORACLE_THIN <dbUserName> <dbPassword> <dbURL>

          Example:
          java -classpath /bea11g/wl_server103/server/lib/weblogic.jar utils.dbping ORACLE_THIN scott tiger localhost:1522:orcl4


          .
          .
          Thanks
          Jay SenSharma
          http://weblogic-wonders.com/weblogic/2010/02/10/jdbc-tips/  (WebLogic Wonders Are Here)
          • 2. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
            Ravi Jegga
            Hi
            As Jay mentioned, make sure that this Datasource is Targetted to AdminServer or Cluster (if cluster domain). The reason is when we create a new Datasource, we go through atleast 4 to 5 screens and only in the LAST screen we see option to Target this to Admin Server or any other servers. So if you click Finish is between only, then it may not be targetted. So from Admin Console, make sure it is Targetted to proper server. Incase if connection pool like username/password/ip or host is not mentioned correctly then you will get some other error. You will not get JNDI Lookup failed. Instead it may say like unable to connect to db like that.

            Also, when you create Datasource in the very first screen there are 2 fileds. One is like Datasource Name and second is JNDI Name. They can be same or different also. But when you do lookup make sure you give JNDI name and not the datasource name.

            Thanks
            Ravi Jegga
            • 3. Re: javax.naming.NameNotFoundException  exception when look up my Datasourc
              dvohra21
              Create a JSP/servlet or JSF client application and in the JSP/JSF obtain the connection as follows.
              InitialContext ctx=new InitialContext();
                  DataSource ds=(DataSource)ctx.lookup("OracleConnection1");
                  Connection connection=ds.getConnection();
              • 4. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
                TakhteJamshid
                Thank you

                I didn't know that I have to target it to AdminServer , I did it and my problem solved

                please let me to ask another question

                Now I wanna to use proxy user I found am example that say use this method to connect through proxy user in JDBC

                InitialContext ic = new InitialContext();
                DataSource nativeDS =
                +(DataSource) ic.lookup(NATIVE_DS_NAME);+
                OracleConnection oconn = (OracleConnection)
                nativeDS.getConnection(PROXY_USER_NAME, PROXY_USER_PWD);

                My DataSource connect through "ali" user and I created proxy user for it by name "proxy_ali" and password "proxy_ali"
                in sqlplus I can connect by "SQL> connect proxy_ali[ali]/proxy_ali"

                application code is like this :

                ---------------------------------------------------------------------------------
                Hashtable env = new Hashtable();
                env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
                env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
                try{
                Context context=new InitialContext( env );
                javax.sql.DataSource ds=(javax.sql.DataSource) context.lookup ("OracleConnection1");
                OracleConnection conn=(OracleConnection) ds.getConnection("proxy_ali", "proxy_ali");

                -----------------------------------------------------------------------------------------------

                this line OracleConnection conn=(OracleConnection) ds.getConnection("proxyali", "proxy_ali")_ raises this exception :

                java.sql.SQLException: User: proxy_ali, failed to be authenticated.
                     at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:234)
                     at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348)
                     at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259)
                     at weblogic.jdbc.common.internal.RmiDataSource_1032_WLStub.getConnection(Unknown Source)
                     at oracle.HRFacadeClient.testJDBC(HRFacadeClient.java:71)
                     at oracle.HRFacadeClient.main(HRFacadeClient.java:20)
                Caused by: java.sql.SQLException: User: proxy_ali, failed to be authenticated.
                     at weblogic.jdbc.common.internal.RmiDataSource.getSubject(RmiDataSource.java:320)
                     at weblogic.jdbc.common.internal.RmiDataSource.getConnection(RmiDataSource.java:252)
                     at weblogic.jdbc.common.internal.RmiDataSource_WLSkel.invoke(Unknown Source)
                     at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:589)
                     at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
                     at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:477)
                     at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
                     at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
                     at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:473)
                     at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
                     at weblogic.work.ExecuteThread.execute(ExecuteThread.java:201)
                     at weblogic.work.ExecuteThread.run(ExecuteThread.java:173)




                And I test that if I change those line to _OracleConnection conn=(OracleConnection) ds.getConnection();_
                it raises this error :
                java.lang.ClassCastException: weblogic.jdbc.rmi.SerialConnection_weblogic_jdbc_rmi_internal_ConnectionImpl_weblogic_jdbc_wrapper_JTAConnection_weblogic_jdbc_wrapper_XAConnection_oracle_jdbc_driver_LogicalConnection_1032_WLStub cannot be cast to oracle.jdbc.driver.OracleConnection
                     at oracle.HRFacadeClient.testJDBC(HRFacadeClient.java:71)
                     at oracle.HRFacadeClient.main(HRFacadeClient.java:20)



                I think I have two problem here

                1-I can't use proxy user
                2-I can't use oracle.jdbc.driver.OracleConnection

                Can you help me ?

                thank you
                • 5. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
                  Ravi Jegga
                  Hi Takhte
                  1. I am glad it is working now once you target to AdminSever. YES, you do need to target to AdminServer and any applicable clusters.

                  2. I am not much familiar with this ProxyUser. But I would like to share some thoughts.

                  3. So, first of all you created a DataSource with JNDI Name say MyJNDI1. During this point you create Connection pool and assume you gave Oracle DB username/password as user1/pwd1. NOW when you do JNDI Lookup and get InitialContext and give this DataSource JNDI Name, you already got connected to the Database. Now getting connected to the database, you are now trying to get connection with different username/password. I am not sure if this may work.
                  DataSource ds = (DataSource) aContext.lookup (dataSourceName);
                  Connection aConnection = ds.getConnection();

                  4. One Workaround I see is, when you create the Datasource in the very first place from Weblogic Console, why don't you give this proxyuser/proxypassword itself for the db details. I guess you are able to connect using SQLPlus using this proxy user, so use the same while creating the datasource also. Then do the JNDI Lookup and above 2 lines to directly get Connection object.

                  5. Another workaround which is totally not recommended is to use low level way to connect to the database. I would not recommend this, but this may definetly work. But not that, proxy user/pwd needs to be exposed either in the java code or in .properties file (if you use static properties file to store db detals like db url, username, password etc)
                  String thinConn = "jdbc:oracle:thin:@host:port:sdiname"
                  String databaseUser = "rjegga";
                  String databasePassword = "rjegga";

                  DriverManager.registerDriver(new OracleDriver());
                  aConnection = DriverManager.getConnection(thinConn, databaseUser, databasePassword);

                  Again the above method is not recommended.

                  Finally I have only one question. You configured datasource with one set of user to connect to the database. BUT while getting this db connection using JNDI, why do you want to pass different set of user like proxy user. If you want to restrict access to specific schema/users like one schema multiple users. But each user can access only specific set of tables. Then create 2 Datasources with 2 diff set of users or proxy users. Then use appropriate Datasource in your Java Code.

                  If I misunderstood any of your requirements, please give more details.

                  Thanks
                  Ravi Jegga
                  • 6. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
                    TakhteJamshid
                    Hello Ravi Jegga

                    the advantage of proxy user is that I can authenticate users by database , and I can audit there actions in database , and I can more control them in database

                    My problem in using OracleConnection solved but my problem in using proxy user is persistence yet , I think it's better I write my problem in another thread because character of my problem has been changed



                    thank you
                    • 7. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
                      dvohra21
                      Also specify the username and password.
                      env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
                      env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");
                      env.put(Context.SECURITY_PRINCIPAL, "weblogic" );
                      env.put(Context.SECURITY_CREDENTIALS, "weblogic");
                      • 8. Re: javax.naming.NameNotFoundException  exception when look up my Datasource
                        TakhteJamshid
                        Hello

                        I find out the solution

                        OracleConnection conn=null;
                        javax.sql.DataSource ds=null;
                        Hashtable env = new Hashtable();
                        env.put( Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory" );
                        env.put(Context.PROVIDER_URL, "t3://127.0.0.1:7001");


                        try{
                        Context context=new InitialContext( env );
                        ds=(javax.sql.DataSource) context.lookup ("OracleConnection2");
                        conn=(OracleConnection) ds.getConnection();
                        java.util.Properties prop = new java.util.Properties();
                        prop.put(OracleConnection.PROXY_USER_NAME, "web_user1");
                        prop.put(OracleConnection.PROXY_USER_PASSWORD,"web_user1");
                        conn.openProxySession(OracleConnection.PROXYTYPE_USER_NAME, prop);