1 2 Previous Next 15 Replies Latest reply on Mar 15, 2007 2:51 AM by Gopinath Govindaraman

    JNDI Problem

    Gopinath Govindaraman Newbie

      Hi,

      I had a standalone test program which lookup JNDI datasource configured in oracle-ds.xml under jboss 3.2.7.

      Since it resides outside of Jboss JVM, the lookup is not happening.

      Is their any way to get it? Or suggest me how can I test it?

      Your comments are highly appreciated.

      Thanks in advance,
      Gopi

        • 1. Re: JNDI Problem
          jaikiran pai Master

          Have a look at:

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

          However, this is not a recommended approach as mentioned at

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

          Note: JBoss does not recommend using this feature on a production environment. It requires accessing a connection pool remotely and this is an anti-pattern as connections are not serializable. Besides, transaction propagation is not supported and it could lead to connection leaks if the remote clients are unreliable (i.e crashes, network failure). If you do need to access a datasource remotely, JBoss recommends accessing it via a remote session bean facade.


          • 2. Re: JNDI Problem
            Gopinath Govindaraman Newbie

            Thanks for the immediate response.

            I am new to EJB.

            Can you please provide me a link to get the datasource using remote session bean facade?

            Thank you,
            Gopi

            • 3. Re: JNDI Problem
              jaikiran pai Master

               

              I am new to EJB.


              Frankly speaking, it would difficult to explain or understand this without going through some sample EJB applications. Once you understand EJB, this will be an easy task to use the remote facade.

              So if its not a production application that you are currently working on, you can get it working by adding

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


              in your -ds.xml file

              • 4. Re: JNDI Problem
                Gopinath Govindaraman Newbie

                I am using Jboss 3.2.7. It is not working.

                I got the below error while lookup. Help me to resolve.

                
                javax.naming.NameNotFoundException: Detects not bound
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:490)
                 at org.jnp.server.NamingServer.getBinding(NamingServer.java:498)
                 at org.jnp.server.NamingServer.getObject(NamingServer.java:504)
                 at org.jnp.server.NamingServer.lookup(NamingServer.java:277)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:606)
                 at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:568)
                 at javax.naming.InitialContext.lookup(InitialContext.java:351)
                 at com.ingenix.itg.framework.util.JNDIUtils.lookup(JNDIUtils.java:79)
                 at com.ingenix.itg.framework.dao.JNDISessionFactoryProvider.getSession(JNDISessionFactoryProvider.java:83)
                 at com.ingenix.itg.framework.dao.JNDISessionFactoryProvider.getSession(JNDISessionFactoryProvider.java:58)
                 at com.ingenix.itg.detects.lookup.dao.LookupDAOImpl._findAllClients(LookupDAOImpl.java:386)
                 at com.ingenix.itg.detects.lookup.dao.LookupDAOImpl.findAllClients(LookupDAOImpl.java:367)
                 at com.ingenix.itg.detects.lookup.LookupManagerImpl.findAllClients(LookupManagerImpl.java:190)
                 at example1.Test2.setupClass(Test2.java:30)
                 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
                 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
                 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
                 at java.lang.reflect.Method.invoke(Method.java:585)
                 at org.testng.internal.MethodHelper.invokeMethod(MethodHelper.java:645)
                 at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:385)
                 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:141)
                 at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:79)
                 at org.testng.TestRunner.beforeRun(TestRunner.java:504)
                 at org.testng.TestRunner.run(TestRunner.java:472)
                 at org.testng.SuiteRunner.runTest(SuiteRunner.java:278)
                 at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:273)
                 at org.testng.SuiteRunner.privateRun(SuiteRunner.java:253)
                 at org.testng.SuiteRunner.run(SuiteRunner.java:168)
                 at org.testng.TestNG.createAndRunSuiteRunners(TestNG.java:987)
                 at org.testng.TestNG.runSuitesLocally(TestNG.java:951)
                 at org.testng.TestNG.run(TestNG.java:719)
                 at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:73)
                 at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:122)
                


                • 5. Re: JNDI Problem
                  jaikiran pai Master

                  Can you post your -ds.xml and the code where you are looking up the datasource.

                  • 6. Re: JNDI Problem
                    Gopinath Govindaraman Newbie

                    Steps
                    1. Included <use-java-context>false</use-java-context> in -ds.xml
                    2. Started Jboss
                    3. Ran standalone program to lookup the datasource. Error occurred.


                    Jboss Server Console
                    11:01:38,343 INFO [DatasourceConnectionProvider] Using datasource: java:/Detects
                    11:01:38,359 INFO [TransactionFactoryFactory] Transaction strategy: net.sf.hibernate.transaction.JTATransactionFactory
                    11:01:38,359 INFO [NamingHelper] JNDI InitialContext properties:{}
                    11:01:38,359 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBossTransactionManagerLookup
                    11:01:38,359 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                    11:01:38,359 INFO [NamingHelper] JNDI InitialContext properties:{}
                    11:01:38,359 INFO [TransactionManagerLookupFactory] instantiating TransactionManagerLookup: net.sf.hibernate.transaction.JBossTransactionManagerLookup
                    11:01:38,359 INFO [TransactionManagerLookupFactory] instantiated TransactionManagerLookup
                    11:01:44,109 INFO [SettingsFactory] Use scrollable result sets: true
                    11:01:44,109 INFO [SettingsFactory] Use JDBC3 getGeneratedKeys(): false
                    11:01:44,109 INFO [SettingsFactory] Optimize cache for minimal puts: false
                    11:01:44,109 INFO [SettingsFactory] Query language substitutions: {}
                    11:01:44,109 INFO [SettingsFactory] cache provider: net.sf.hibernate.cache.TreeCacheProvider
                    11:01:44,109 INFO [Configuration] instantiating and configuring caches
                    11:01:44,156 INFO [SessionFactoryImpl] building session factory
                    11:01:50,187 INFO [SessionFactoryObjectFactory] Factory name: java:/hibernate/SessionFactory
                    11:01:50,187 INFO [NamingHelper] JNDI InitialContext properties:{}
                    11:01:50,203 INFO [NamingHelper] Creating subcontext: hibernate
                    11:01:50,203 INFO [SessionFactoryObjectFactory] Bound factory to JNDI name: java:/hibernate/SessionFactory
                    11:01:50,203 WARN [SessionFactoryObjectFactory] InitialContext did not implement EventContext
                    11:01:50,203 INFO [NamingHelper] JNDI InitialContext properties:{}
                    


                    LookupTest Source
                    package example1;
                    
                    import java.util.Hashtable;
                    
                    import javax.naming.Context;
                    import javax.naming.InitialContext;
                    
                    public class LookupTest
                    {
                     public static void main(String[] args) throws Exception
                     {
                     Hashtable props = new Hashtable();
                     props.put(Context.INITIAL_CONTEXT_FACTORY,
                     "org.jnp.interfaces.NamingContextFactory");
                     props.put(Context.PROVIDER_URL, "jnp://localhost:1099");
                     props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
                    
                     InitialContext ic = new InitialContext(props);
                     Object lookupObj = ic.lookup("java:/Detects");
                     System.out.println("Object:" + lookupObj);
                     }
                    }
                    


                    Error details while lookup
                    Exception in thread "main" javax.naming.NameNotFoundException: Detects not bound
                     at org.jnp.server.NamingServer.getBinding(NamingServer.java:490)
                     at org.jnp.server.NamingServer.getBinding(NamingServer.java:498)
                     at org.jnp.server.NamingServer.getObject(NamingServer.java:504)
                     at org.jnp.server.NamingServer.lookup(NamingServer.java:277)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:606)
                     at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:568)
                     at javax.naming.InitialContext.lookup(InitialContext.java:351)
                     at example1.LookupTest.main(LookupTest.java:19)
                    


                    Tried JNDI name "java:/Detects" and "Detects". It is not working.

                    • 7. Re: JNDI Problem
                      Gopinath Govindaraman Newbie

                      Forgot to add -ds.xml file


                      <?xml version="1.0" encoding="UTF-8"?>
                      <datasources>
                       <local-tx-datasource>
                       <jndi-name>Detects</jndi-name>
                       <driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
                       <connection-url>jdbc:oracle:thin:@XXX:1521:XXXX</connection-url>
                       <user-name>XXXX</user-name>
                       <password>XXXX</password>
                       <exception-sorter-class-name>
                       org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter
                       </exception-sorter-class-name>
                       <use-java-context>false</use-java-context>
                       </local-tx-datasource>
                      </datasources>


                      • 8. Re: JNDI Problem
                        jaikiran pai Master

                        Apologies for neglecting the fact, so far, that you have been using JBoss-3.2.x. This does not work in JBoss-3.x. This only works with JBoss-4. Let me see if i can compile a simple remote session bean for you to get this working.

                        • 9. Re: JNDI Problem
                          jaikiran pai Master

                          Here's the remote session bean facade which you can use in JBoss-3.2.x to work with datasources from the standalone client:

                          Home interface:

                          package org.myapp.ejb;
                          
                          import java.rmi.RemoteException;
                          import javax.ejb.CreateException;
                          import javax.ejb.EJBHome;
                          
                          /**
                           *
                           * @author Jaikiran Pai
                           *
                           */
                          public interface HelloWorldHome extends EJBHome {
                          
                           public HelloWorldRemote create() throws RemoteException, CreateException;
                          
                          }


                          Remote interface:

                          package org.myapp.ejb;
                          
                          import java.rmi.RemoteException;
                          import javax.ejb.EJBObject;
                          
                          /**
                           *
                           * @author Jaikiran Pai
                           *
                           */
                          public interface HelloWorldRemote extends EJBObject {
                          
                           public void workWithDatabase(String dataSourceName) throws RemoteException;
                          
                          }


                          The bean class:

                          package org.myapp.ejb;
                          
                          import java.rmi.RemoteException;
                          
                          import javax.ejb.EJBException;
                          import javax.ejb.SessionBean;
                          import javax.ejb.SessionContext;
                          import javax.naming.Context;
                          import javax.naming.InitialContext;
                          import javax.naming.NamingException;
                          import javax.sql.DataSource;
                          
                          /**
                           *
                           * @author Jaikiran Pai
                           *
                           */
                          public class HelloWorldBean implements SessionBean {
                          
                           /**
                           * Session context
                           */
                           private SessionContext sessionCtx;
                          
                           /**
                           * Default constructor
                           *
                           */
                           public HelloWorldBean() {
                          
                          
                           }
                          
                           /**
                           * @see javax.ejb.SessionBean#setSessionContext(javax.ejb.SessionContext)
                           */
                           public void setSessionContext(SessionContext sessionCtx) throws EJBException, RemoteException {
                           this.sessionCtx = sessionCtx;
                           }
                          
                           public void ejbActivate() throws EJBException {
                          
                           }
                          
                           public void ejbPassivate() throws EJBException {
                           }
                          
                           public void ejbRemove() throws EJBException {
                           }
                          
                          
                           public void ejbCreate() {
                           }
                          
                           /**
                           *
                           * @param dataSourceName
                           * @throws RemoteException
                           */
                           public void workWithDatabase(String dataSourceName) throws RemoteException {
                          
                           try {
                           Context ctx = new InitialContext();
                           DataSource dataSource = (DataSource) ctx.lookup(dataSourceName);
                           System.out.println("Got datasource: " + dataSource);
                           /*
                           * TODO: Do something with the datasource
                           */
                          
                           } catch (NamingException ne) {
                           /*
                           * TODO: Do exception handling
                           */
                           ne.printStackTrace();
                           throw new RemoteException("Exception while working with datasource",ne);
                           }
                          
                           }
                          
                          }


                          ejb-jar.xml

                          <?xml version="1.0" encoding="UTF-8"?>
                          <!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN" 'http://java.s
                          un.com/dtd/ejb-jar_2_0.dtd'>
                          
                          <ejb-jar >
                          
                           <description><![CDATA[No Description.]]></description>
                           <display-name>Sample EJB Application</display-name>
                          
                           <enterprise-beans>
                          
                           <!-- Session Beans -->
                           <session >
                          
                           <ejb-name>HelloWorldBean</ejb-name>
                           <remote>org.myapp.ejb.HelloWorldRemote</remote>
                           <home>org.myapp.ejb.HelloWorldHome</home>
                           <ejb-class>org.myapp.ejb.HelloWorldBean</ejb-class>
                           <session-type>Stateless</session-type>
                           <transaction-type>Container</transaction-type>
                          
                          
                           </session>
                          
                          
                           </enterprise-beans>
                          
                           <!-- Assembly Descriptor -->
                           <assembly-descriptor >
                          
                           <!-- transactions -->
                           <container-transaction >
                           <method >
                           <ejb-name>HelloWorldBean</ejb-name>
                           <method-name>*</method-name>
                           </method>
                           <trans-attribute>required</trans-attribute>
                           </container-transaction>
                           </assembly-descriptor>
                          
                          
                          
                          </ejb-jar>


                          jboss.xml:

                          <?xml version="1.0" encoding="UTF-8"?>
                          <!DOCTYPE jboss PUBLIC "-//JBoss//DTD JBOSS 3.0//EN" "http://www.jboss.org/j2ee/dtd/jboss_3_0.dtd">
                          
                          <jboss>
                           <enterprise-beans>
                           <session>
                           <ejb-name>HelloWorldBean</ejb-name>
                           <jndi-name>HelloWorld</jndi-name>
                           </session>
                           </enterprise-beans>
                          </jboss>


                          The standalone class:

                          package org.myapp.client;
                          
                          import javax.naming.Context;
                          import javax.naming.InitialContext;
                          import javax.rmi.PortableRemoteObject;
                          
                          import org.myapp.ejb.HelloWorldHome;
                          import org.myapp.ejb.HelloWorldRemote;
                          
                          
                          
                          /**
                           *
                           * @author Jaikiran Pai
                           *
                           */
                          public class TestClient {
                          
                           public static void main(String args[]) {
                           try {
                           Context ctx = new InitialContext();
                           Object obj = ctx.lookup("HelloWorld"); HelloWorldHome home = (HelloWorldHome) PortableRemoteObject.narrow(obj,HelloWorldHome.class);
                           HelloWorldRemote helloWorldRemote = home.create();
                          
                           helloWorldRemote.workWithDatabase("java:/DefaultDS");
                           System.out.println("Done with the datasource");
                          
                           } catch (Exception e) {
                           /*
                           * TODO: Do exception handling
                           */
                           e.printStackTrace();
                           }
                           }
                          }


                          You will have learn EJB to understand this. Good luck :-)



                          • 10. Re: JNDI Problem
                            Gopinath Govindaraman Newbie

                            It really helped me. Thanks a lot. I will try to implement this.

                            In btw, I have a small question.

                            How can i invoke Jboss service from standalone application?

                            I had hibernate-service and want to invoke that service from standalone application. This will solve almost all my problem.

                            Appreiciate your help in this regard

                            -Gopi

                            • 11. Re: JNDI Problem
                              jaikiran pai Master

                              Your hibernate service will be deployed as a MBean. You can get a remote access to your MBean as mentioned in section "Using the RMIADaptor" at http://wiki.jboss.org/wiki/Wiki.jsp?page=HowDoIGetRemoteAccessToMyMBean

                              • 12. Re: JNDI Problem
                                Gopinath Govindaraman Newbie

                                I will give it a try.

                                • 13. Re: JNDI Problem
                                  Adil berdai Newbie

                                  Hi !
                                  Could you like please send me jboss-service.xml releated to deploying this application ? I have a problem of MBeanServer (ConnectionManager) !

                                  Thank you in advance

                                  • 14. Re: JNDI Problem
                                    Adil berdai Newbie

                                    Do you stil have the jboss-service.xml file ?
                                    Thanks in advance !

                                    1 2 Previous Next