9 Replies Latest reply: Feb 10, 2010 1:08 PM by Michael Hughes RSS

Error Using CMT "EntityManager must be access within a transaction"

Michael Hughes Newbie

Hi,

 

I am trying to use CMT with EJB3 SLSB & Hibernate EntityManager. The problem appears to be that when I call the DAO to persist an entity it doesn't join the current transaction. I know this looks like a Hibernate problem but I am wondering whether there is some specific piece of configuration I am missing to get CMT working in JBoss?

 

I have an EJB that calls the persist() method on an EJB DAO. Both classes/methods are annotated with...

 

     @TransactionAttribute(TransactionAttributeType.REQUIRED)

 

 

Stack Trace:
2010-02-07 19:13:53,233 ERROR [ie.eread.ebook.service.ebook.management.impl.DeploymentValidatorImpl] (http-127.0.0.1-8080-1) Failed to find URL [http://localhost:8080/ebook-data/ebooks/29320/preview/flipbook/index.html]
2010-02-07 19:13:56,879 ERROR [org.jboss.aspects.tx.TxPolicy] (http-127.0.0.1-8080-1) javax.ejb.EJBTransactionRolledbackException: EntityManager must be access within a transaction
2010-02-07 19:14:02,686 ERROR [org.jboss.aspects.tx.TxPolicy] (http-127.0.0.1-8080-1) javax.ejb.EJBTransactionRolledbackException: EntityManager must be access within a transaction
2010-02-07 19:14:02,717 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[localhost].[/jmx-console].[HtmlAdaptor]] (http-127.0.0.1-8080-1) Servlet.service() for servlet HtmlAdaptor threw exception
javax.persistence.TransactionRequiredException: EntityManager must be access within a transaction
     at org.jboss.jpa.deployment.ManagedEntityManagerFactory.verifyInTx(ManagedEntityManagerFactory.java:155)
     at org.jboss.jpa.tx.TransactionScopedEntityManager.persist(TransactionScopedEntityManager.java:186)
     at ie.eread.ebook.dao.impl.GenericDAOImpl.persist(GenericDAOImpl.java:38)
     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:597)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
     at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
     at sun.reflect.GeneratedMethodAccessor282.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_7955372.invoke(InvocationContextInterceptor_z_fillMethod_7955372.java)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_7955372.invoke(InvocationContextInterceptor_z_setup_7955372.java)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
     at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:194)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:176)
     at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:216)
     at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
     at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
     at $Proxy359.persist(Unknown Source)
     at ie.eread.ebook.service.ebook.management.impl.DeploymentProcessorImpl.processDeploymentPersistStatus(DeploymentProcessorImpl.java:92)
     at ie.eread.ebook.service.ebook.management.impl.DeploymentProcessorImpl.processDeployments(DeploymentProcessorImpl.java:76)
     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:597)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
     at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
     at sun.reflect.GeneratedMethodAccessor282.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_7955372.invoke(InvocationContextInterceptor_z_fillMethod_7955372.java)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_7955372.invoke(InvocationContextInterceptor_z_setup_7955372.java)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.tx.TxPolicy.invokeInCallerTx(TxPolicy.java:126)
     at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:194)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:176)
     at org.jboss.ejb3.session.SessionSpecContainer.invoke(SessionSpecContainer.java:216)
     at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:207)
     at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:164)
     at $Proxy329.processDeployments(Unknown Source)
     at ie.eread.ebook.service.jmx.impl.EbookManagementMBeanImpl.deployEbooks(EbookManagementMBeanImpl.java:36)
     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:597)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
     at org.jboss.ejb3.EJBContainerInvocationWrapper.invokeNext(EJBContainerInvocationWrapper.java:69)
     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:73)
     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:59)
     at sun.reflect.GeneratedMethodAccessor282.invoke(Unknown Source)
     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
     at java.lang.reflect.Method.invoke(Method.java:597)
     at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:72)
     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_7955372.invoke(InvocationContextInterceptor_z_fillMethod_7955372.java)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:88)
     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_7955372.invoke(InvocationContextInterceptor_z_setup_7955372.java)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.tx.TxPolicy.invokeInOurTx(TxPolicy.java:79)
     at org.jboss.aspects.tx.TxInterceptor$Required.invoke(TxInterceptor.java:190)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:186)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
     at org.jboss.ejb3.service.ServiceContainer.localInvoke(ServiceContainer.java:451)
     at org.jboss.ejb3.service.ServiceMBeanDelegate.invoke(ServiceMBeanDelegate.java:215)
     at org.jboss.mx.server.RawDynamicInvoker.invoke(RawDynamicInvoker.java:164)
     at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:668)
     at org.jboss.jmx.adaptor.control.Server.invokeOpByName(Server.java:258)
     at org.jboss.jmx.adaptor.control.Server.invokeOp(Server.java:223)
     at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet$3.run(HtmlAdaptorServlet.java:380)
     at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet$3.run(HtmlAdaptorServlet.java:378)
     at java.security.AccessController.doPrivileged(Native Method)
     at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.invokeOp(HtmlAdaptorServlet.java:376)
     at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.invokeOp(HtmlAdaptorServlet.java:287)
     at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.processRequest(HtmlAdaptorServlet.java:104)
     at org.jboss.jmx.adaptor.html.HtmlAdaptorServlet.doPost(HtmlAdaptorServlet.java:86)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
     at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
     at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
     at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
     at java.lang.Thread.run(Thread.java:619)

 

 

 

persistence.xml:


<persistence 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_1_0.xsd"
                version="1.0">

<persistence-unit name="ebooks" transaction-type="JTA">
     <provider>org.hibernate.ejb.HibernatePersistence</provider>
     <jta-data-source>java:/DefaultDS</jta-data-source>
     <class>ie.eread.ebook.model.Category</class>
     <class>ie.eread.ebook.model.CategoryEbook</class>
     <class>ie.eread.ebook.model.CurrStatus</class>
     <class>ie.eread.ebook.model.Ebook</class>
     <class>ie.eread.ebook.model.EbookAltTitle</class>
     <class>ie.eread.ebook.model.EbookDwnld</class>
     <class>ie.eread.ebook.model.EbookDwnldDet</class>
     <class>ie.eread.ebook.model.EbookPattern</class>
     <class>ie.eread.ebook.model.Language</class>
     <class>ie.eread.ebook.model.Person</class>
     <class>ie.eread.ebook.model.PersonRole</class>
     <class>ie.eread.ebook.model.PersonRoleEbook</class>
     <class>ie.eread.ebook.model.Subject</class>
     <class>ie.eread.ebook.model.SubjectEbook</class>
     <class>ie.eread.ebook.model.User</class>
     <class>ie.eread.ebook.model.UserDwnld</class>
     <properties>
          <property name="hibernate.archive.autodetection" value="class"/>
          <!-- <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> -->
          <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
          <property name="hibernate.transaction.manager_lookup_class" 
                      value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
          <property name="hibernate.transaction.auto_close_session" value="true"/>
          <property name="hibernate.transaction.flush_before_completion" value="true"/>
<!-- 
          <property name="hibernate.transaction.factory_class" 
                      value="org.hibernate.transaction.CMTTransactionFactory"/>
 -->
      </properties>
</persistence-unit>

</persistence>

 

I had to comment out the "hibernate.transaction.factory_class" property as I was getting a "Do not override..." warning message each time I called a DAO method even though I have read in JBoss documentation that this property should be set.

 

The actual factory class instantiated is (according to server.log)..

 

2010-02-06 13:24:58,747 INFO  [org.hibernate.transaction.TransactionFactoryFactory] (main) Transaction strategy: org.hibernate.ejb.transaction.JoinableCMTTransactionFactory
2010-02-06 13:24:58,760 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] (main) instantiating TransactionManagerLookup: org.hibernate.transaction.JBossTransactionManagerLookup
2010-02-06 13:24:58,770 INFO  [org.hibernate.transaction.TransactionManagerLookupFactory] (main) instantiated TransactionManagerLookup

 

I know alot of this is Hibernate related but I just want to be sure I have my JBoss configuration correct.

 

Regards,

 

Michael

  • 1. Re: Error Using CMT "EntityManager must be access within a transaction"
    Michael Hughes Newbie

    Hi,

     

    Could someone pls take a look at this as our project is on hold until we can get CMT working.

     

    Regards,

     

    Michael

  • 3. Re: Error Using CMT "EntityManager must be access within a transaction"
    jaikiran pai Master

    Your config looks OK to me. Except that you can get rid of  the hiberrnate.transaction.manager_lookup_class property from the persistence.xml. Can you post the relevant bits of your code? Including how you get hold of the entity manager?

  • 4. Re: Error Using CMT "EntityManager must be access within a transaction"
    Michael Hughes Newbie

    Jaikiran,

     

    Thanks for your reply.

     

    I use a generic DAO class where the entity manager is injected:

     

    package ie.eread.ebook.dao.impl;
    
    import ie.eread.ebook.dao.api.GenericDAO;
    
    import java.io.Serializable;
    import java.lang.reflect.ParameterizedType;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    public abstract class GenericDAOImpl<T, ID extends Serializable> 
                                  implements GenericDAO<T, ID>
    {
         @PersistenceContext
         private EntityManager em;
         private Class<T> persistentClass;
              
         @SuppressWarnings("unchecked")
         public GenericDAOImpl()
         {
              this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
                   .getGenericSuperclass()).getActualTypeArguments()[0];
         }
         
         public T findById(ID id)
         {
              return (T) getEntityManager().find(getPersistentClass(), id);
         }
    
         public void persist(T entity) 
         {
              getEntityManager().persist(entity);
         }
         
         public T merge(T entity) 
         {
              return (T) getEntityManager().merge(entity);
         }
    
         public void delete(T entity) 
         {
              getEntityManager().remove(entity);
         }
         public void flush()
         {
              getEntityManager().flush();
         }
         
         public void clear()
         {
              getEntityManager().clear();
         }
    
         public void setPersistentClass(Class<T> persistentClass)
         {
              this.persistentClass = persistentClass;
         }
         
         public Class<T> getPersistentClass()
         {
              return persistentClass;
         }
         
         public void setEntityManager(EntityManager em)
         {
              this.em = em;
         }
         public EntityManager getEntityManager()
         {
              return em;
         }
    }
    
    

     

    The DAO implementation class:

     

    package ie.eread.ebook.dao.impl;
    
    import ie.eread.ebook.dao.api.EbookDAO;
    import ie.eread.ebook.model.Ebook;
    
    import java.util.List;
    
    import javax.ejb.Stateless;
    import javax.ejb.TransactionAttribute;
    import javax.ejb.TransactionAttributeType;
    import javax.ejb.TransactionManagement;
    import javax.ejb.TransactionManagementType;
    import javax.persistence.Query;
    
    @Stateless(name="EbookDAO")
    @TransactionManagement(TransactionManagementType.CONTAINER)
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public class EbookDAOImpl extends GenericDAOImpl<Ebook, Long> 
                              implements EbookDAO 
    {
         
         @SuppressWarnings("unchecked")
         public Ebook getEbookBySrcFileRef(String srcFileRef)
         {          
              Query q = getEntityManager().createNamedQuery("Ebook.bySrcFileRef");
              q.setParameter("srcFileRef", srcFileRef);
              q.setMaxResults(1);
              List<Ebook> result = q.getResultList();
              if (result == null) return null;
              if (result.isEmpty()) return null;
              return (Ebook) q.getResultList().get(0);
         }
    
         @SuppressWarnings("unchecked")
         public List<Ebook> getEbooksByPersonId(Long personId) {
              Query q = getEntityManager().createNamedQuery("Ebook.byPersonId");
              q.setParameter("personId", personId);
              return q.getResultList();
         }
    
         @SuppressWarnings("unchecked")
         public List<Ebook> getEbooksByAuthorName(String startName, String lastName) {
              Query q = getEntityManager().createNamedQuery("Ebook.byPersonName");
              q.setParameter("startName", startName);
              q.setParameter("lastName", lastName);
              return q.getResultList();
         }
    
         @SuppressWarnings("unchecked")
         public List<Ebook> getEbooksBySubjectId(Integer subjId) {
              Query q = getEntityManager().createNamedQuery("Ebook.bySubjId");
              q.setParameter("subjId", subjId);
              return q.getResultList();
         }
    
         @SuppressWarnings("unchecked")
         public List<Ebook> getEbooksByCategoryCd(String catCd) {
              Query q = getEntityManager().createNamedQuery("Ebook.byCatCd");
              q.setParameter("catCd", catCd);
              return q.getResultList();
         }
    
         @SuppressWarnings("unchecked")
         public Ebook getEbookByTitle(String title) {
              Query q = getEntityManager().createNamedQuery("Ebook.byTitle");
              q.setParameter("title", title);
              q.setMaxResults(1);
              List<Ebook> result = q.getResultList();
              if (result == null) return null;
              if (result.isEmpty()) return null;
              return (Ebook) q.getResultList().get(0);
         }
    
         @SuppressWarnings("unchecked")
         public List<Ebook> getAllEbooksFetchAll() {
              Query q = getEntityManager().createNamedQuery("Ebook.allFetchAll");
              return q.getResultList();
         }
    
         @SuppressWarnings("unchecked")
         public List<Ebook> getEbooksByStatus(String currStatLabel)
         {
              Query q = getEntityManager().createNamedQuery("Ebook.byStatus");
              q.setParameter("currStatLabel", currStatLabel);
              return q.getResultList();
         }
    
    }
    
    

     

    The EJB that calls the DAO:

     

    package ie.eread.ebook.service.ebook.management.impl;
    
    import java.util.List;
    
    import ie.eread.ebook.dao.api.EbookDAO;
    import ie.eread.ebook.model.Ebook;
    import ie.eread.ebook.model.enums.ECurrStatus;
    import ie.eread.ebook.service.ebook.management.api.DeploymentPersister;
    import ie.eread.ebook.service.ebook.management.api.DeploymentProcessor;
    import ie.eread.ebook.service.ebook.management.api.DeploymentValidator;
    import ie.eread.ebook.service.exception.DeploymentPersistenceException;
    import ie.eread.ebook.service.exception.DeploymentProcessorException;
    import ie.eread.ebook.service.exception.DeploymentValidationException;
    import ie.eread.ebook.util.ebook.DeploymentListener;
    import ie.eread.ebook.util.exception.DeploymentListenerException;
    
    import javax.annotation.Resource;
    import javax.ejb.*;
    
    import org.apache.log4j.Logger;
    
    @Stateless(name = "DeploymentProcessor")
    @TransactionManagement(TransactionManagementType.CONTAINER)
    public class DeploymentProcessorImpl implements DeploymentProcessor
    {
         @Resource SessionContext context;
         
         @EJB(mappedName="EbookDAO/local")
         private EbookDAO eBookDAO;
    
         @EJB(mappedName="DeploymentValidator/local")
         private DeploymentValidator deploymentValidator;
    
         @EJB(mappedName="DeploymentPersister/local")
         private DeploymentPersister deploymentPersister;
    
         private static DeploymentListener deploymentListener;
    
         private int successCnt;
    
         private Logger logger = Logger.getLogger(DeploymentProcessorImpl.class.getName());
    
         public DeploymentProcessorImpl()
         {
         }
    
         /**
          * Processes all ebooks flagged for deployment. This method cannot have
          * transaction demarcation as this could cause a rollback for successfully
          * deployed ebooks.
          */
         public int processDeployments()
         {
              logger.info("Searching for ebooks with status ['"+ 
                   ECurrStatus.AwDplymntCheck.getLabel() +"']");
              
              List<Ebook> deployedEbooks = 
                   eBookDAO.getEbooksByStatus(ECurrStatus.AwDplymntCheck.getLabel());
              logger.info("["+ deployedEbooks.size() +"] ebooks found waiting for deployment check");
              
              if (deployedEbooks.size() == 0)
              {
                   logger.info("No further work required here - bailing out...");
                   return 0;
              }
              
              successCnt = 0;
              for (Ebook ebook : deployedEbooks)
              {
                   processDeploymentPersistStatus(ebook);
              }
              return successCnt;
         }
         
         @TransactionAttribute(TransactionAttributeType.REQUIRED)
         public void processDeploymentPersistStatus(Ebook ebook)
         {
              try
              {
                   processDeployment(ebook);
                   ebook.setCurrStatus(ECurrStatus.Active);
                   eBookDAO.persist(ebook);
              } catch (DeploymentProcessorException e)
              {
                   ebook.setCurrStatus(ECurrStatus.FailedDplymntCheck);
                   eBookDAO.persist(ebook);
              }
         }
    
         @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
         public void processDeployment(Ebook ebook) throws DeploymentProcessorException
         {
              DownloadFileDetails dfd;
              try
              {
                   dfd = deploymentValidator.validateDeployment(ebook);
                   deploymentPersister.persistDownloadDetails(ebook, dfd);
                   if (deploymentListener != null) deploymentListener.deploy(ebook);          
              } catch (DeploymentValidationException e)
              {
                   context.setRollbackOnly();
                   throw new DeploymentProcessorException(e);
              } catch (DeploymentPersistenceException e)
              {
                   context.setRollbackOnly();
                   throw new DeploymentProcessorException(e);
              } catch (DeploymentListenerException e)
              {
                   context.setRollbackOnly();
                   throw new DeploymentProcessorException(e);
              }
              
         }
    
         @SuppressWarnings("static-access")
         public void setDeploymentListener(DeploymentListener deploymentListener)
         {
              this.deploymentListener = deploymentListener;
         }
    
    }
    
    

     

    The "Entity Manager must be accessed within transaction" Exception is thrown when I call ebookDAO.persist(ebook).

     

    Any help would be much appreciated.

     

    Regards,

     

    Michael

  • 5. Re: Error Using CMT "EntityManager must be access within a transaction"
    jaikiran pai Master

    I haven't completely looked at the code, but on a initial glance, you seem to be calling the persist (even) in the catch block. i.e. when the tx might have been rolledback or marked for rollback. Does the line number in the exception stacktrace, that you posted, match with that persist call in the catch block?

  • 6. Re: Error Using CMT "EntityManager must be access within a transaction"
    Michael Hughes Newbie

    Yes I know. But the first method...

    TransactionAttributeType.REQUIRED
    processDeploymentPersistStatus(Ebook ebook)
    ...should start a new transaction (as one doesn't exist at this stage). And the second method...
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void processDeployment(Ebook ebook) throws DeploymentProcessorException
    ...should suspend the first transaction and start a new one. So I expected there to be 2 seperate transactions. If you could give me any tips for the best way to debug this that would be very helpful. I've tried setting log4j hibernation.transaction to DEBUG but don't get any logging information.
  • 7. Re: Error Using CMT "EntityManager must be access within a transaction"
    Michael Hughes Newbie

    Sorry, didn't answer your question. The line number in the stack trace corresponds to the 2nd call to HEM persist which is in the catch block of 1st method:

              } catch (DeploymentProcessorException e)
              {
                   ebook.setCurrStatus(ECurrStatus.FailedDplymntCheck);
                   eBookDAO.persist(ebook);
              }

    I would expect the 1st transaction to still be active at this stage.

  • 8. Re: Error Using CMT "EntityManager must be access within a transaction"
    jaikiran pai Master

    dobreden2 wrote:

     

    Yes I know. But the first method...

    TransactionAttributeType.REQUIRED
    processDeploymentPersistStatus(Ebook ebook)
    ...should start a new transaction (as one doesn't exist at this stage). And the second method...
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void processDeployment(Ebook ebook) throws DeploymentProcessorException
    ...should suspend the first transaction and start a new one.

    The call to the second method from the first one is a plain java call, so no new transaction is created and instead it will continue with the existing one. If you want the new transaction semantics to apply on the call to processDeployment, then you have to invoke that method on the business object of the bean. So something like:

     

    ...
    public class DeploymentProcessorImpl implements DeploymentProcessor
    {
    ...
    @Resource
    private javax.ejb.SessionContext sessionContext;
    ...
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public void processDeploymentPersistStatus(Ebook ebook)
    {
              try
              {
            // get business object
            DeploymentProcessor processor = this.sessionContext.getBusinessObject(DeploymentProcessor.class);
            // invoke on that business object
                   processor.processDeployment(ebook);
    
                   ebook.setCurrStatus(ECurrStatus.Active);
                   eBookDAO.persist(ebook);
              } catch (DeploymentProcessorException e)
              {
                   ebook.setCurrStatus(ECurrStatus.FailedDplymntCheck);
                   eBookDAO.persist(ebook);
              }
    }
     
    @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
    public void processDeployment(Ebook ebook) throws DeploymentProcessorException
    {
    ...
    
  • 9. Re: Error Using CMT "EntityManager must be access within a transaction"
    Michael Hughes Newbie

    Thanks alot - Worked a treat. I have a couple of books on EJB3 and have scanned the internet for days but found no reference to this approach.

     

    Regards,

     

    Michael