4 Replies Latest reply on Apr 26, 2007 6:05 AM by wolfc

    EntityManager Null when trying to inject into stateless sess

    bmant

      Hi,

      I'm having an issue deploying to JBoss 4.0.5-ejb3. When I try to inject my EntityManager in my code:

      @PersistenceContext EntityManager em;
      

      it is always null. However, if I do:
       EntityManagerFactory emf = Persistence.createEntityManagerFactory("myDS");
      em = emf.createEntityManager();
      


      This is my setup:

      I have a java web service that calls my Stateless Session bean. In my session bean, I try to inject the PersistenceContext, so I can utilize my Entity Beans.

      I have a .ear archive that I deploy to the server/default/deploy directory (using JBuilder 2007) - with a .war (that contains my web service) and a .jar that contains my beans (entity and session).

      I have everything annotated according to the documentation. All of the documentation says that it should work.
      ISampleClass is the @Local Interface class.

      @Stateless (name="SampleClass")
      public class SampleClass implements ISampleClass
      
       @PersistenceContext (unitName="myDS")
       EntityManager em;
      
       public Resource findResource() {
       Resource resource = em.find(Resource.class, 1);
       return resource;
       }
      }
      


      I've also tried:
      @TransactionAttribute (TransactionAttributeType.REQUIRED)
      at the class and method level.


      I wrote a client that calls the web service (Axis 1.3 client generated). It calls the service fine, but I get a NullPointerException when it tries to call the em.persist() method inside my stateless session bean.

      Here is my persistence.xml file:
      ><persistence-unit name="myDS">
       <provider>org.hibernate.ejb.HibernatePersistence</provider>
       <jta-data-source>java:/myDS</jta-data-source>
       <properties>
       <property name="hibernate.hbm2ddl.auto" value="validate"></property>
       <property name="hibernate.dialect"
       value="org.hibernate.dialect.MySQLInnoDBDialect" />
       <property name="hibernate.session_factory_name"
       value="java:hibernate/HibernateFactory" />
       <property name="transaction.factory_class"
       value="org.hibernate.transaction.JTATransactionFactory" />
       <property name="hibernate.cache.use_second_level_cache"
       value="false" />
       <property name="hibernate.transaction.manager_lookup_class"
       value="org.hibernate.transaction.JBossTransactionManagerLookup" />
       </properties>
       </persistence-unit>
      


      I've also tried explicity setting the transaction-type to "JTA" at the persistence-unit level, not setting the provider and leaving the properties section empty.

      Here is my mysql-ds.xml file:
      <datasources>
       <local-tx-datasource>
       <jndi-name>myDS</jndi-name>
       <connection-url>jdbc:mysql://localhost:3306/myDB</connection-url>
       <driver-class>com.mysql.jdbc.Driver</driver-class>
       <user-name>myuser</user-name>
       <password>mypass</password>
       <connection-property name="socketFactory">com.mysql.jdbc.NamedPipeSocketFactory</connection-property>
       <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
       <metadata>
       <type-mapping>mySQL</type-mapping>
       </metadata>
       </local-tx-datasource>
      </datasources
      


      And looking at the JBoss logs with debug on I see this (which seems to me that it's loading correctly):


      2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] starting service jboss.j2ee:service=EJB3,module=MyApp.jar
      2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.Ejb3Module] Starting jboss.j2ee:service=EJB3,module=MyApp.jar
      2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.JmxKernelAbstraction] creating wrapper delegate for: org.jboss.ejb3.entity.PersistenceUnitDeployment
      2007-04-24 09:00:51,656 INFO [org.jboss.ejb3.JmxKernelAbstraction] installing MBean: persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS with dependencies
      2007-04-24 09:00:51,656 INFO [org.jboss.ejb3.JmxKernelAbstraction] jboss.jca:name=pmacDS,service=DataSourceBinding
      2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] Creating service persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
      2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] adding depends in ServiceController.register: [jboss.jca:name=myDS,service=DataSourceBinding]
      2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] recording that persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS depends on jboss.jca:name=pmacDS,service=DataSourceBinding
      2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.ServiceDelegateWrapper] Creating persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
      2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.ServiceDelegateWrapper] Created persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
      2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] Creating dependent components for: persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS dependents are: []
      2007-04-24 09:00:51,656 DEBUG [org.jboss.system.ServiceController] starting service persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
      2007-04-24 09:00:51,656 DEBUG [org.jboss.ejb3.ServiceDelegateWrapper] Starting persistence.units:ear=MyEar.ear,jar=MyApp.jar,unitName=myDS
      2007-04-24 09:00:51,671 DEBUG [org.jboss.ejb3.entity.PersistenceUnitDeployment] Found persistence.xml file in EJB3 jar
      2007-04-24 09:00:51,687 INFO [org.hibernate.ejb.Version] Hibernate EntityManager 3.2.0.GA
      2007-04-24 09:00:51,703 INFO [org.hibernate.cfg.annotations.Version] Hibernate Annotations 3.2.0.GA
      2007-04-24 09:00:51,718 INFO [org.hibernate.cfg.Environment] Hibernate 3.2.0.ga
      2007-04-24 09:00:51,734 INFO [org.hibernate.cfg.Environment] hibernate.properties not found
      2007-04-24 09:00:51,734 INFO [org.hibernate.cfg.Environment] Bytecode provider name : javassist
      2007-04-24 09:00:51,734 INFO [org.hibernate.cfg.Environment] using JDK 1.4 java.sql.Timestamp handling
      2007-04-24 09:00:51,828 DEBUG [org.hibernate.ejb.Ejb3Configuration] 2007-04-20 09:31:26,390 DEBUG [org.hibernate.ejb.Ejb3Configuration] Processing PersistenceUnitInfo [
      name: myDS
      persistence provider classname: org.hibernate.ejb.HibernatePersistence
      classloader: org.jboss.mx.loading.UnifiedClassLoader3@174a144{ url=file:/C:/jboss-4.0.5.GA/server/default/tmp/deploy/tmp30571MyEAR.ear ,addedOrder=47}
      Temporary classloader: null
      excludeUnlistedClasses: false
      JTA datasource: org.jboss.resource.adapter.jdbc.WrapperDataSource@4eb98b
      Non JTA datasource: null
      Transaction type: JTA
      PU root URL: file:/C:/jboss-4.0.5.GA/server/default/tmp/deploy/tmp30571MyEAR.ear-contents/MyJAR.jar
      Jar files URLs []
      Managed classes names [ <list of my classes>]
      Mapping files names []
      Properties [
      hibernate.jacc.ctx.id: MyJAR.jar
      hibernate.jndi.java.naming.factory.initial: org.jnp.interfaces.NamingContextFactory
      hibernate.transaction.manager_lookup_class: org.hibernate.transaction.JBossTransactionManagerLookup
      hibernate.cache.provider_class: org.hibernate.cache.HashtableCacheProvider
      hibernate.dialect: org.hibernate.dialect.MySQLInnoDBDialect
      hibernate.bytecode.use_reflection_optimizer: false
      hibernate.jndi.java.naming.factory.url.pkgs: org.jboss.naming:org.jnp.interfaces
      hibernate.bytecode.provider: javassist


      Any ideas?

      Thanks,

      Bryan

        • 1. Re: EntityManager Null when trying to inject into stateless
          alrubinger

          I couldn't say for sure, but a flag to me is that you're putting your EJBs in the WAR. I usually separate them into their own JARs with a:

          <module>
           <ejb>ejbs-name.jar</ejb>
           </module>


          ...in META-INF/application.xml so the EJB3 Deployer knows to scan for them.

          S,
          ALR

          • 2. Re: EntityManager Null when trying to inject into stateless
            bmant

            Thanks for the fast response - but I am separating them - below is my application.xml.

            <?xml version="1.0" encoding="UTF-8"?>
            <application id="Application_ID" version="1.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/application_1_4.xsd">
             <display-name>
             MyEar</display-name>
             <module id="WebModule_1175089155093">
             <web>
             <web-uri>FindResourcesService.war</web-uri>
             <context-root>FindResourcesService</context-root>
             </web>
             </module>
             <module id="EjbModule_1175102889765">
             <ejb>MyEjbs.jar</ejb>
             </module>
            


            I just did a little research - and I'm able to get the PersistenceContext injected - if I access my Stateless bean with a stand-alone java client (using InitialContext.lookup - and such).

            So I think my issue is when a stand-alone java client - calls the webservice - then the webservice tries to create an instance of the Stateless session bean... I have to look into that now.

            Do I have to add some Context persistence into my service call? I'll continue searching.

            my client to call the service is this:

            FindResourcesServiceLocator locator = new FindResourcesServiceLocator();
            FindResourcesServicePort port = locator.getFindResourcesServicePort();
            port.findResource()
            


            and my webservice just initializes the SampleClass and calls a method in it:
            SampleClass sample = new SampleClass();
            sample.findResource();
            




            • 3. Re: EntityManager Null when trying to inject into stateless
              bmant

              Here is an update:

              I was able to inject the PersistenceContext into my session bean by retrieving the LocalInterface class from the InitialContext within my web service java class.

              SampleClass sc = null;
              InitialContext ic = new InitialContext();
              sc = (ISampleClass)ic.lookup("MyEar/SampleClass/local");
              


              I tried injecting the EJB at the class level - but that was always null.

              @EJB (mappedName="MyEar/SampleClass/local")
              SampleClass sc;
              


              I also tried just setting the mapped name to: SampleClass, MyEar/SampleClass and a bunch of other things - but none of them worked.

              Is this the optimal solution to get my web service an instance of my stateless session bean from the container?