7 Replies Latest reply on Feb 9, 2017 6:55 AM by gprade

    NullPointerException by @PersistenceContext(unitName = "java:/defaultPU") EntityManager in WildFly-8.1.0.FINAL

    hermann778

      Hello *,

      i learn now how to use WildFly-8.1.0.Final with MySql. I use a sample "multiple-pu" from the javaee7-samples (javaee-samples/javaee7-samples · GitHub)

      The entitymanager from my stateless session bean has a value null after injection. WildFly admin console shows that the datasource configuration standalone.xml after "Test Connection" is successful.


      Error from Terminal:

      [org.jboss.weld.deployer] (MSC service thread 1-7) JBAS016008: Starting weld service for deployment multiple-pu.war

      08:48:59,961 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-11) JBAS017534: Registered web context: /multiple-pu

      08:48:59,993 INFO  [org.jboss.as.server] (ServerService Thread Pool -- 27) JBAS018559: Deployed "multiple-pu.war" (runtime-name : "multiple-pu.war")

      08:48:59,999 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management

      08:48:59,999 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990

      08:48:59,999 INFO  [org.jboss.as] (Controller Boot Thread) JBAS015874: WildFly 8.1.0.Final "Kenny" started in 3687ms - Started 310 of 362 services (91 services are lazy, passive or on-demand)

      08:56:42,850 ERROR [org.jboss.as.ejb3.invocation] (default task-1) JBAS014134: EJB Invocation failed on component MySessionBean for method public java.util.List org.javaee7.jpa.multiple.pu.MySessionBean.listMovies(): javax.ejb.EJBException: java.lang.NullPointerException

        at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:190) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:275) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:340) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:239) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ejb3.component.interceptors.CurrentInvocationContextInterceptor.processInvocation(CurrentInvocationContextInterceptor.java:41) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ejb3.component.invocationmetrics.WaitTimeInterceptor.processInvocation(WaitTimeInterceptor.java:43) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ejb3.security.SecurityContextInterceptor.processInvocation(SecurityContextInterceptor.java:95) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ejb3.component.interceptors.ShutDownInterceptorFactory$1.processInvocation(ShutDownInterceptorFactory.java:64) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ejb3.component.interceptors.LoggingInterceptor.processInvocation(LoggingInterceptor.java:59) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50)

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:55) [wildfly-ejb3-8.1.0.Final.jar:8.1.0.Final]

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.invocation.ContextClassLoaderInterceptor.processInvocation(ContextClassLoaderInterceptor.java:64)

        at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:309)

        at org.jboss.invocation.InterceptorContext.run(InterceptorContext.java:326)


      Datasource configuration in standalone.xml looks like:

      <datasource jta="true" jndi-name="java:/defaultPU" pool-name="defaultPU" enabled="true" use-java-context="true" use-ccm="true">

                          <connection-url>jdbc:mysql://localhost:3306/dms</connection-url>

                          <driver>mysql</driver>

                          <pool>

                              <min-pool-size>10</min-pool-size>

                              <max-pool-size>100</max-pool-size>

                              <prefill>true</prefill>

                              <use-strict-min>false</use-strict-min>

                              <flush-strategy>FailingConnectionOnly</flush-strategy>

                          </pool>

                          <security>

                              <user-name>dms</user-name>

                              <password>dms</password>

                          </security>

                          <statement>

                              <prepared-statement-cache-size>100</prepared-statement-cache-size>

                              <share-prepared-statements>true</share-prepared-statements>

                          </statement>

                      </datasource>

                      <datasource jta="true" jndi-name="java:/samplePU" pool-name="samplePU" enabled="true" use-java-context="true" use-ccm="true">

                          <connection-url>jdbc:mysql://localhost:3306/dms</connection-url>

                          <driver>mysql</driver>

                          <pool>

                              <min-pool-size>10</min-pool-size>

                              <max-pool-size>100</max-pool-size>

                              <prefill>true</prefill>

                              <use-strict-min>false</use-strict-min>

                              <flush-strategy>FailingConnectionOnly</flush-strategy>

                          </pool>

                          <security>

                              <user-name>dms</user-name>

                              <password>dms</password>

                          </security>

                      </datasource>

                      <drivers>

                          <driver name="h2" module="com.h2database.h2">

                              <xa-datasource-class>org.h2.jdbcx.JdbcDataSource</xa-datasource-class>

                          </driver>

                          <driver name="mysql" module="com.mysql"/>

                      </drivers>

       

       

      Persistence.xml

      <persistence-unit name="defaultPU" transaction-type="JTA">

              <jta-data-source>java:/defaultPU</jta-data-source>

              <properties>

                  <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

                  <property name="javax.persistence.schema-generation.create-source" value="script"/>

                  <property name="javax.persistence.schema-generation.drop-source" value="script"/>

                  <property name="javax.persistence.schema-generation.create-script-source" value="META-INF/create.sql"/>

                  <property name="javax.persistence.schema-generation.drop-script-source" value="META-INF/drop.sql"/>

                  <property name="javax.persistence.sql-load-script-source" value="META-INF/load.sql"/>

              </properties>

          </persistence-unit>

       

       

          <persistence-unit name="samplePU" transaction-type="JTA">

             <jta-data-source>java:/samplePU</jta-data-source>

          </persistence-unit>

      MySessionBean.java:

      @Stateless

      public class MySessionBean {

       

       

          @PersistenceContext(unitName = "java:/defaultPU")

          EntityManager defaultEM;

       

       

          @PersistenceContext(unitName = "java:/samplePU")

          EntityManager sampleEM;

       

       

          public List<Movie> listMovies() {

              return defaultEM.createNamedQuery("Movie.findAll", Movie.class).getResultList();

          }

         

          public List<ProductCode> listProductCode() {

              return sampleEM.createNamedQuery("ProductCode.findAll", ProductCode.class).getResultList();

          }

       

       

      }

      defaultEM is null on the line "return defaultEM.createNamedQuery("Movie.findAll", Movie.class).getResultList();"  when the method listMovies() is invoked from the servlet like this:

      @Inject MySessionBean bean;

      ....

      ....

       

      bean.listMovies();

      ....

       

       

      I miss something in my configuration.

      Thank you for any help and sorry if this forum is not right place for my problem.

      Hermann