5 Replies Latest reply on Apr 18, 2007 2:06 PM by Alex Komlev

    JNDI, JDBC & Simple Application

    Alex Komlev Newbie

      Help me, please.

      I put this code in Servlet:

      InitialContext jndiContext = new InitialContext();
      Object obj = jndiContext.lookup("java:PostgresDS");
      DataSource ds = (DataSource) PortableRemoteObject.narrow(obj, DataSource.class);
      ...
      


      and its work correctly. But then I put this code in application, Eclipse write:

      javax.naming.NameNotFoundException: (PostgresDS not bound ...

      In what difference? Why it not work in application?



        • 1. Re: JNDI, JDBC & Simple Application
          Elias Ross Master

          What do you mean by application? Is the servlet not your application?

          The full exception is needed to see.

          Check out

          http://jboss.org/wiki/Wiki.jsp?page=DisplayTheJDNITreeWithTheJMXConsole

          • 2. Re: JNDI, JDBC & Simple Application
            Weston M. Price Master

            A few things:

            1) A DataSource does not require a PortableRemoteObject.narrow(). This is typically used for EJB resources running outside of the container (ie thin client).

            The real question you are facing is the 'location' of the DataaSource. Code running within the application server can simply access the DataSource as follows:

            
            InitialContext jndiContext = new InitialContext();
            DataSource ds = (DataSource)jndiContext.lookup("java:DSName");
            
            


            Note, it's better to use a resource-ref but that is for another discussion :-)

            Code, running outside of JBoss wanting to access a DataSource needs to add the following element to the *-ds.xml file

            <use-java-context>false</use-java-context>
            


            At this point you can access the DataSource remotely be executing

            
            InitialContext jndiContext = new InitialContext();
            DataSource ds = (DataSource)jndiContext.lookup("DSName");
            
            


            Note the difference in the JNDI name. Remote DataSource access uses the Global JNDI namespace, not the ENC namespace reserved for J2EE application components.

            A *BIG* word of caution here. DataSources are typicall not meant to be used outside of an application server. Testing simply JDBC interactions is fine but you are strongly, strongly, strongly encouraged not to do this in production environment. More on this can be found here:

            http://wiki.jboss.org/wiki/Wiki.jsp?page=ConfigDataSources



            • 3. Re: JNDI, JDBC & Simple Application
              Alex Komlev Newbie

              Big thanks for your help. I change -ds.xml and use instead of "java:PostgresDS", "PostgresDS". In connection stage i don't have visible problems,
              but on

              ResultSet rs = st.executeQuery("SELECT * FROM TestTable");
              


              i see

              Exception in thread "main" java.lang.reflect.UndeclaredThrowableException
              at $Proxy2.execute(Unknown Source)
              at org.ConnectFromJndi.main(ConnectFromJndi.java:36)
              Caused by: java.rmi.UnmarshalException: Error unmarshaling return; nested exception is:
              java.lang.ClassNotFoundException: org.postgresql.util.PSQLException (no security manager: RMI class loader disabled)
              at sun.rmi.transport.StreamRemoteCall.executeCall(Unknown Source)
              at sun.rmi.server.UnicastRef.invoke(Unknown Source)
              at org.jboss.invocation.jrmp.server.JRMPInvoker_Stub.invoke(Unknown Source)
              at org.jboss.invocation.jrmp.interfaces.JRMPInvokerProxy.invoke(JRMPInvokerProxy.java:133)
              at org.jboss.invocation.InvokerInterceptor.invokeInvoker(InvokerInterceptor.java:365)
              at org.jboss.invocation.InvokerInterceptor.invoke(InvokerInterceptor.java:197)
              at org.jboss.proxy.ClientMethodInterceptor.invoke(ClientMethodInterceptor.java:74)
              at org.jboss.resource.adapter.jdbc.remote.StatementInterceptor.invoke(StatementInterceptor.java:58)
              at org.jboss.proxy.ClientContainer.invoke(ClientContainer.java:100)
              ... 2 more
              Caused by: java.lang.ClassNotFoundException: org.postgresql.util.PSQLException (no security manager: RMI class loader disabled)
              at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
              at sun.rmi.server.LoaderHandler.loadClass(Unknown Source)
              at java.rmi.server.RMIClassLoader$2.loadClass(Unknown Source)
              at java.rmi.server.RMIClassLoader.loadClass(Unknown Source)
              at sun.rmi.server.MarshalInputStream.resolveClass(Unknown Source)
              at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
              at java.io.ObjectInputStream.readClassDesc(Unknown Source)
              at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
              at java.io.ObjectInputStream.readObject0(Unknown Source)
              at java.io.ObjectInputStream.readObject(Unknown Source)
              ... 11 more



              Maybe its my errors. After your message I am declined to not use jndi and will be limited simple JDBC to connection. It is a lot of complexities and not enough practical advantage in my case.

              • 4. Re: JNDI, JDBC & Simple Application
                Weston M. Price Master

                This simply means that you don't have the PostGres JDBC jar file in your classpath. Add that and you should be ok.

                • 5. Re: JNDI, JDBC & Simple Application
                  Alex Komlev Newbie

                  I'm stupid - to forget such simple thing. All works. :-)
                  Small thing - if my table's name is TestTable? it's don't work, if name is testtable - all work. Request don't changing - "SELECT * FROM TestTable"
                  Magic :-)