8 Replies Latest reply on Oct 11, 2012 2:18 PM by kieras1

    Different implementation instances of EntityManager returned by different "retrieving" methods

    Newbie

      Hi,

       

      I have a EJB with CMT that persist an object with JPA, in JBoss AS 7.1.1.Final.

       

      If I inject the EntityManager with @PersistenceContext, the actual instance of EntityManager is org.jboss.as.jpa.container.TransactionScopedEntityManager.

      If I inject the EntityManagerFactory with @PersistenceUnit and then create an EntityManager from it, the actual instance of EntityManager is org.hibernate.ejb.EntityManagerImpl.

      If I do a JNDI lookup on EntityManagerFactory and then create an EntityManager from it, the actual instance of EntityManager also is org.hibernate.ejb.EntityManagerImpl.

       

      Does anyone know why I am obtaining different implementation instances of EntityManager?

       

      Thanks,

      Kieras.

        • 1. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
          Nicklas Karlsson Master

          With EE and injection, you usually get a proxy that only is guaranteed to satisfy the interface or method signatures of the target. Depening on the context (transactions etc), different classes might be provided for you.

          • 2. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
            Newbie

            Ok, I understood.

            However, when the EntityManager is obtained from the injected (or looked up) EntityManagerFactory, I was getting the error: "javax.persistence.TransactionRequiredException: no transaction is in progress".

            And, actually, there was a CMT transaction in progress, but it seems like the org.hibernate.ejb.EntityManagerImpl was somewhat not aware of.

            Is this the correct behaviour? It seems strange to me.

             

            Sorry I didn't have posted the code or stacktrace. I refactored the code to inject the EntityManager directly, solved that TransactionRequiredException problem, and moved on.

             

            Thanks again.

            • 3. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
              Nicklas Karlsson Master

              I haven't looked at the implementation but objects are seldom aware of anything automagically, they are helped by interceptors etc that join them to transactions etc.

              • 4. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
                Scott Marlow Master

                Could you attach your persistence.xml or its contents here.

                 

                Also, could you recreate the problem with TRACE logging enabled for "org.jboss.as.jpa", "org.hibernate" and "com.arjuna" and attach the server.log output here as well (including enough activity before the TransactionRequiredException occurs and after so we can understand what is going on

                 

                Instructions for enabling TRACE logging are here.

                • 5. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
                  Newbie

                  Here is te persistence.xml:

                   

                  {code:xml}

                  <?xml version="1.0" encoding="UTF-8"?>

                  <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">

                            <persistence-unit name="grp-contabil-postegres-ds" transaction-type="JTA">

                                      <provider>org.hibernate.ejb.HibernatePersistence</provider>

                                      <jta-data-source>java:/abc-module-postegres-ds</jta-data-source>

                                      ...

                                      <properties> 

                                                <property name="jboss.entity.manager.factory.jndi.name" value="java:/emf" />

                                                <property name="hibernate.hbm2ddl.auto" value="update" />

                                          <property name="hibernate.show_sql" value="false" /> 

                                          <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect" />

                                     <property name="hibernate.ejb.resource_scanner" value="org.hibernate.ejb.packaging.NativeScanner" />

                                            <property name="hibernate.default_schema" value="GDRD"/>

                                     <property name="show_sql" value="false"/>

                                      </properties>

                            </persistence-unit>

                  </persistence>

                  {code}

                   

                   

                  Here is the error stacktrace:

                   

                  {noformat}

                  10:19:33,619 ERROR [org.jboss.ejb3.invocation] (Thread-81) JBAS014134: EJB Invocation failed on component abc-module-ejb for method public abstract void br.gov.fw.comuns.IProFacade.gravar(br.gov.fw.entidades.ProBaseVO) throws java.lang.Exception: javax.ejb.EJBException: javax.persistence.TransactionRequiredException: no transaction is in progress

                            at org.jboss.as.ejb3.tx.CMTTxInterceptor.handleExceptionInOurTx(CMTTxInterceptor.java:166) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:230) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.as.ejb3.tx.CMTTxInterceptor.required(CMTTxInterceptor.java:304) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.as.ejb3.tx.CMTTxInterceptor.processInvocation(CMTTxInterceptor.java:190) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

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

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

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

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ee.component.NamespaceContextInterceptor.processInvocation(NamespaceContextInterceptor.java:50) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ejb3.component.interceptors.AdditionalSetupInterceptor.processInvocation(AdditionalSetupInterceptor.java:32) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ee.component.TCCLInterceptor.processInvocation(TCCLInterceptor.java:45) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ee.component.ViewService$View.invoke(ViewService.java:165) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.as.ee.component.ViewDescription$1.processInvocation(ViewDescription.java:173) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ee.component.ProxyInvocationHandler.invoke(ProxyInvocationHandler.java:72) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at br.gov.fw.comuns.IProFacadeLocal$$$view1.gravar(Unknown Source) [fw-interfaces.jar:]

                            at br.gov.fw.controle.ProCtr.salvar(ProCtr.java:430) [fw-web.jar:]

                            at br.gov.fw.visao.ProHelperView$1.onEvent(ProHelperView.java:422) [fw-web.jar:]

                            at org.zkoss.zk.ui.impl.EventProcessor.process0(EventProcessor.java:206) [zkmodificado.jar:]

                            at org.zkoss.zk.ui.impl.EventProcessor.process(EventProcessor.java:140) [zkmodificado.jar:]

                            at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.process0(EventProcessingThreadImpl.java:517) [zkmodificado.jar:]

                            at org.zkoss.zk.ui.impl.EventProcessingThreadImpl.run(EventProcessingThreadImpl.java:444) [zkmodificado.jar:]

                  Caused by: javax.persistence.TransactionRequiredException: no transaction is in progress

                            at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:970) [hibernate-entitymanager-4.0.1.Final.jar:4.0.1.Final]

                            at br.gov.fw.persistencia.ProBaseDAO.gravar(ProBaseDAO.java:117) [fw-dao.jar:]

                            at br.gov.fw.negocio.ProBaseRN.gravar(ProBaseRN.java:56) [fw-ejb.jar:]

                            at br.gov.fw.negocio.ProFacadeImp.gravar(ProFacadeImp.java:39) [fw-ejb.jar:]

                            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.6.0_29]

                            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [rt.jar:1.6.0_29]

                            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [rt.jar:1.6.0_29]

                            at java.lang.reflect.Method.invoke(Method.java:597) [rt.jar:1.6.0_29]

                            at org.jboss.as.ee.component.ManagedReferenceMethodInterceptorFactory$ManagedReferenceMethodInterceptor.processInvocation(ManagedReferenceMethodInterceptorFactory.java:72) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.invocation.WeavedInterceptor.processInvocation(WeavedInterceptor.java:53) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ee.component.interceptors.UserInterceptorFactory$1.processInvocation(UserInterceptorFactory.java:36) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.jpa.interceptor.SBInvocationInterceptor.processInvocation(SBInvocationInterceptor.java:47) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.invocation.InitialInterceptor.processInvocation(InitialInterceptor.java:21) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.invocation.ChainedInterceptor.processInvocation(ChainedInterceptor.java:61) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ee.component.interceptors.ComponentDispatcherInterceptor.processInvocation(ComponentDispatcherInterceptor.java:53) [jboss-as-ee-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ejb3.component.pool.PooledInstanceInterceptor.processInvocation(PooledInstanceInterceptor.java:51) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            at org.jboss.invocation.InterceptorContext.proceed(InterceptorContext.java:288) [jboss-invocation-1.1.1.Final.jar:1.1.1.Final]

                            at org.jboss.as.ejb3.tx.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:228) [jboss-as-ejb3-7.1.1.Final.jar:7.1.1.Final]

                            ... 26 more

                  {noformat}

                   

                   

                  The code that is getting the EntityManager is using an "antique" style , I know, but I inherited it.

                  Actually I am doing an architectural reconstruction and refatoring the system to use more modern and good practices.

                   

                  {code}

                  public class ProDAOHelper {

                   

                            private static final ThreadLocal<EntityManager> entityManagerThreadLocal = new ThreadLocal<EntityManager>();

                            private static EntityManagerFactory emf = null;

                   

                            static {

                                try {

                                           Context context = new javax.naming.InitialContext();

                                           emf = (EntityManagerFactory) context.lookup("java:/emf");

                                 } catch (NamingException e) {

                                           e.printStackTrace();

                                 }

                            }

                   

                            public static EntityManager entityManager() {

                                 if (entityManagerThreadLocal.get() == null) {

                                        EntityManager em = emf.createEntityManager();

                                         entityManagerThreadLocal.set(em);

                                 }

                                 return entityManagerThreadLocal.get();

                            }

                   

                            public static void closeEntityManager() {

                                 EntityManager entityManager = entityManagerThreadLocal.get();

                                 if (entityManager != null && entityManager.isOpen()) {

                                           entityManager.close();

                                 }

                                 entityManagerThreadLocal.set(null);

                            }

                  }

                  {code}

                   

                   

                  And attached is the more complete log output.

                   

                  Thank you!

                  • 6. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
                    Scott Marlow Master

                    Its not related to the issue but you might remove the "hibernate.ejb.resource_scanner" property from the persistence.xml since AS7 should configure that to a VFS enabled scanner.

                     

                    I'll let you know what I see in the attached log...

                    • 7. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
                      Scott Marlow Master

                      Looks like an exception or something caused the JTA transaction to rollback previously

                       

                       

                      10:19:33,432 TRACE [com.arjuna.ats.jtax] (Thread-81) TransactionImple.setRollbackOnly

                      .

                      .

                      .

                      10:19:33,619 ERROR [org.jboss.ejb3.invocation] (Thread-81) JBAS014134: EJB Invocation failed on component abc-module-ejb for method public abstract void br.gov.fw.comuns.IProFacade.gravar(br.gov.fw.entidades.ProBaseVO) throws java.lang.Exception: javax.ejb.EJBException: javax.persistence.TransactionRequiredException: no transaction is in progress

                       

                      You need to figure out why the transaction rolled back at 10:19:33:432, that looks to be the root cause (not a bug in Hibernate which was my concern).

                      1 of 1 people found this helpful
                      • 8. Re: Different implementation instances of EntityManager returned by different "retrieving" methods
                        Newbie

                        I looked again and the application's code is not causing any exception.

                        I have some restrictions in the environment and couldn't debug some of the 3rd party code that might be causing this issue.

                        Anyway, I appreciate very much your help. Thanks!