9 Replies Latest reply on Nov 3, 2007 1:59 PM by bydcc

    jndi jboss service problem connecting

    bydcc

      Hi all,
      I'm trying to connect to the jboss service JNDI from a stand alone application and always I try to lookup an object I get a javax.naming.NameNotFoundException.

      I have deployed two entity ejb3:
      INFO [Ejb3Configuration] found EJB3 Entity bean: com.cartstore.persistence.domain.model.CategoryEntity
      INFO [Ejb3Configuration] found EJB3 Entity bean: com.cartstore.persistence.domain.model.ItemEntity
      ....
      INFO [AnnotationBinder] Binding entity from annotated class: com.cartstore.persistence.domain.model.CategoryEntity
      INFO [EntityBinder] Bind entity com.cartstore.persistence.domain.model.CategoryEntity on table CATEGORY
      INFO [AnnotationBinder] Binding entity from annotated class: com.cartstore.persistence.domain.model.ItemEntity
      INFO [EntityBinder] Bind entity com.cartstore.persistence.domain.model.ItemEntity on table ITEM
      ....

      I have defined a datasource:
      INFO [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=CartStoreDS' to JNDI name 'java:CartStoreDS'
      ....

      I've invoked the method list of JNDIView and I've seen:
      Inside:
      java: Namespace
      Some of them:
      +- timedCacheFactory (class: javax.naming.Context)
      Failed to lookup: timedCacheFactory, errmsg=org.jboss.util.TimedCachePolicy
      +- SecurityProxyFactory (class: org.jboss.security.SubjectSecurityProxyFactory)
      +- CartStoreDS (class: org.jboss.resource.adapter.jdbc.WrapperDataSource)
      +- comp (class: javax.naming.Context)
      +- EntityManagers (class: org.jnp.interfaces.NamingContext)
      | +- cartstore (class: org.jboss.ejb3.entity.TransactionScopedEntityManager)

      I'm using the server jboss-4.2.1.GA and the IDE JBossIDE-2.0.0.Beta2-Bundle-win32.zip

      I've tried to lookup several objects and always have got the same result javax.naming.NameNotFoundException:

      For example. With CartStoreDS I get:
      javax.naming.NameNotFoundException: CartStoreDS not bound
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:529)
      at org.jnp.server.NamingServer.getBinding(NamingServer.java:537)

      And in the console trace when I run jboss I see:
      12:49:40,234 INFO [ConnectionFactoryBindingService] Bound ConnectionManager 'jboss.jca:service=DataSourceBinding,name=CartStoreDS' to JNDI name 'java:CartStoreDS'

      I've tried to lookup a datasource, an EntityManager, TopicConnectionFactory and so on.
      My code is:

      public static void main(String[] args) {
      ....
      try {
      ......
      Hashtable ht = new Hashtable();
      ht.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
      ht.put("java.naming.provider.url", "jnp://localhost:1099");
      ht.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");

      Context ic = new InitialContext(ht);
      //EntityManager em = (EntityManager) ic.lookup("java:/EntityManagers/cartstore");
      DataSource ds= (DataSource) ic.lookup("java:/DefaultDS");
      ....
      } catch(NamingException e) {
      e.printStackTrace();
      }
      }

      When I run jboss I don't see any trace about the port 1099. The only trace I see is:
      INFO [NamingHelper] JNDI InitialContext properties:{java.naming.factory.initial=org.jnp.interfaces.NamingContextFactory, java.naming.factory.url.pkgs=org.jboss.naming:org.jnp.interfaces}

      I tried the same thing out eclipse with identical results.

      What am I doing bad?

      Thank you in advance.

        • 1. Re: jndi jboss service problem connecting
          jaikiran

          The java: namespace is visible only to clients within the same JVM as the server. Remote clients (client in a different JVM), like your standalone application, wont be able to access the java: namespace.

          • 2. Re: jndi jboss service problem connecting
            bydcc

            But I have read that JBoss has a naming service which can be accessed remotely.

            Reference from jboss4-j2ee.pdf:
            2.2. The JBossNS Architecture
            The JBossNS architecture is a Java socket/RMI based implementation of the javax.naming.Context interface. It is
            a client/server implementation that can be accessed remotely. The implementation is optimized so that access from
            within the same VM in which the JBossNS server is running does not involve sockets. Same VM access occurs
            through an object reference available as a global singleton.


            And you have the java.naming.provider.url property to indicate the url and port in which this service will be availabe.

            Am I wrong?

            • 3. Re: jndi jboss service problem connecting
              jaikiran

              The JNDI tree is itself divided into different namespaces. The java: namespace is meant for clients within the same JVM. Then there is the java:comp/env which for each application/component. There is also a Global namespace which is accessible for all (local as well as remote clients). So if you want to lookup a object from a remote client (stand alone application), that objects needs to be present in the Global namespace. All these namespaces and their contents are listed in the JNDI tree view available through jmx-console. By the way, if you are looking for accessing a datasource(which is by default bound to java: namespace) from a standalone client, then have a look at this wiki entry http://wiki.jboss.org/wiki/Wiki.jsp?page=HowCanIAccessADataSourceFromAClient

              • 4. Re: jndi jboss service problem connecting
                bydcc

                Thanks for your help.
                I think I needed to read a lot about this issue.

                Now, I've got to lookup a datasource and got a connection. It was only a test.
                But, I really want to lookup an entity manager for my persistence unit.

                My code is:

                Properties ht = new Properties();
                 ht.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
                 ht.put("java.naming.provider.url", "jnp://localhost:1099");
                 ht.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
                
                 Context ic = new InitialContext(ht);
                
                 Object obj=ic.lookup("cartstore");
                 EntityManager em = (EntityManager) obj;
                
                 CategoryEntity ce = new CategoryEntity();
                 ce.setCreationDate(new Timestamp( Calendar.getInstance().getTimeInMillis() ) );
                 ...
                 em.persist(ce);

                But a I get a null reference to my entity manager.

                Persistence unit definition:
                <persistence>
                 <persistence-unit name="cartstore">
                 <!--jta-data-source>java:/CartStoreDS</jta-data-source-->
                 <jta-data-source>CartStoreDS</jta-data-source>
                 <class>com.cartstore.persistence.domain.model.CategoryEntity</class>
                 <class>com.cartstore.persistence.domain.model.ItemEntity</class>
                
                 <properties>
                 <property name="jboss.entity.manager.jndi.name" value="cartstore"/>
                 <property name="jboss.entity.manager.factory.jndi.name" value="CartstoreFactory"/>
                 </properties>
                 </persistence-unit>
                </persistence>


                jboss.entity.manager.jndi.name property force to jboss to public my persistence unit in global jndi section,
                reference: http://docs.jboss.org/ejb3/app-server/reference/build/reference/en/html/entityconfig.html

                Info from JNDIView:
                Global JNDI Namespace
                ......
                +- persistence.units:unitName=cartstore (class: org.hibernate.impl.SessionFactoryImpl)
                .....
                +- CartStoreDS (class: javax.sql.DataSource)
                ....

                I know that I could test it with a EJB3StandaloneDeployer (JBoss) but I would like to do a fisrt test as simple as posible.

                Any idea of why I am not be able to see my entity manager will be appreciated.

                Thank you in advance.



                • 5. Re: jndi jboss service problem connecting
                  jaikiran

                   

                  "ByDcc" wrote:

                  Global JNDI Namespace
                  ......
                  +- persistence.units:unitName=cartstore (class: org.hibernate.impl.SessionFactoryImpl)
                  .....



                  I personally, haven't tried binding the entitymanager to the JNDI. Going by the JNDI tree contents that you posted, it looks like the JNDI name its bound to is persistence.units:unitName=cartstore. So your lookup code should be

                  Object obj=ic.lookup("persistence.units:unitName=cartstore");


                  Having said that, i am not sure why is persistence.units:unitName is being prefixed to the JNDI name. Give it a try and see if it works.



                  • 6. Re: jndi jboss service problem connecting
                    jaikiran

                     

                    persistence.units:unitName=cartstore (class: org.hibernate.impl.SessionFactoryImpl)


                    And going by the object that is bound to this name, this is the EntityManagerFactory object.

                    • 7. Re: jndi jboss service problem connecting
                      bydcc

                      Well, I tried many names but the result is always the same.

                      Global JNDI Namespace
                      ....
                       +- ConnectionFactory (class: org.jboss.mq.SpyConnectionFactory)
                      ....
                       +- persistence.units:unitName=cartstore (class: org.hibernate.impl.SessionFactoryImpl)
                      ...
                       +- CartstoreFactory (class: org.jboss.ejb3.entity.InjectedEntityManagerFactory)



                      My code
                       Object obj=ic.lookup("cartstore");
                       Object obj1=ic.lookup("persistence.units:unitName=cartstore");
                       Object obj2=ic.lookup("ConnectionFactory");
                      
                       EntityManagerFactory em = (EntityManagerFactory) ic.lookup("CartstoreFactory");
                      
                       CategoryEntity ce = new CategoryEntity();
                       ce.setCreationDate(new Timestamp( Calendar.getInstance().getTimeInMillis() ) );
                       ce.setDescription("pppppp");
                       ce.setKeyName("lllllll");
                       ce.setName("oooooooo");
                      
                       em.createEntityManager().persist(ce);


                      obj is null
                      obj1 is null
                      obj2 is NOT null. I have a reference to an object.
                      And the last line throws a NullPointerException.

                      • 8. Re: jndi jboss service problem connecting
                        jaikiran

                        After reading some of the docs, i guess the reason why you are getting a null object for the entitymanger and the entitymanager factory, might have to do with the fact that these objects are meant to be used in a managed environment and hence are not available to the standalone client. That even explains why you are getting a NULL object (and not NameNotFoundException) during the lookup.

                        P.S: This is just based on what i could find about entity managers and my previous experience on similar issue. Someone who might have a better understanding about EntityManagers might want to confirm this.

                        • 9. Re: jndi jboss service problem connecting
                          bydcc

                          Probably you are right ;-)
                          I really thank for your help.