1

以下是我的环境规范:

服务器:JBOSS6.1 最终版

持久层:以 Hibernate 作为持久提供者的 JPA(Hibernate 版本 3.6.6.Final)

应用架构类似于:Controller -> Session Beans -> Businell Logic Layer -> Builder (Hibernate layer) - DB (MYSQL)

我们正在使用在 persistence.xml 中配置的“RESOURCE_LOCAL”事务,如下所示:

<persistence-unit name="test" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <non-jta-data-source>java:/TestDS</non-jta-data-source>

我们使用如下所示的交易:

try {
  txUtil.beginTransaction();
  <-- some entity update etc here -->
  if(some validation fail here){
      txUtil.rollbackTransaction();
  } else {
      txUtil.commitTransaction();
  } catch (Exception e) {
      txUtil.rollbackTransaction();
  } finally {
      try {
          txUtil.closeEntityManager();
      } catch (BAException e) {
          log.warn("Problem occurred while closing the EntityManager: " + e.getLocalizedMessage(), e);
      }
  }

上面使用的各种方法的代码类似于(下面不是精确的 impl,而是我们所拥有的粗略表示):

beginTransaction(){
    EntityManagerFactory emf = Persistence.createEntityManagerFactory("test");
    EntityManager em = emf.createEntityManager();
    if(em.isOpen())
    {
            transaction = em.getTransaction();
            if(!transaction.isActive())
            {   
                transaction.begin();
            }
    }
}


commitTransaction(){
    if(em.isOpen())
    {
        EntityTransaction transaction = em.getTransaction();
        if(transaction.isActive())
        {   
            transaction.commit();
        }
    }
}

rollbackTransaction(){
    if(em.isOpen())
    {
        EntityTransaction transaction = em.getTransaction();
        if(transaction.isActive())
        {   
            log.debug("EntityManager for thread (" + 
                Thread.currentThread().hashCode() +
                ") has active transaction (" + 
                transaction.hashCode() + "): rollback");
            transaction.rollback();
        }
    }
}

closeEntityManager() {
    if(em.isOpen()) {
        EntityTransaction tx=em.getTransaction();
        if(tx==null) {
           log.debug("No transaction associated with EntityManager");
        } else {
            if(tx.isActive()) {
               log.debug("EntityManager has active transaction, rolling out");
               tx.rollback();
            } else {
               log.debug("EntityManager transaction was not active");
            }
        }
     } try {
        em.close();
     }
}

问题是,每当我们进行回滚,然后如果我们尝试关闭实体管理器,它就会引发回滚异常。相同的堆栈跟踪如下:

04:46:16,378 ERROR [STDERR] org.hibernate.TransactionException: could not register synchronization
04:46:16,384 ERROR [STDERR]     at org.hibernate.transaction.JTATransaction.registerSynchronization(JTATransaction.java:316)
04:46:16,385 ERROR [STDERR]     at org.hibernate.ejb.EntityManagerImpl.close(EntityManagerImpl.java:125)
04:46:16,386 ERROR [STDERR]     at com.test.builder.util.EntityManagerUtil.rollbackAndCloseEntityManager(EntityManagerUtil.java:201)
04:46:16,387 ERROR [STDERR]     at com.test.builder.util.TransactionUtilImpl.closeEntityManager(TransactionUtilImpl.java:40)
04:46:16,391 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
04:46:16,392 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
04:46:16,393 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
04:46:16,394 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:597)
04:46:16,395 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeTarget(MethodInvocation.java:122)
04:46:16,396 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:111)
04:46:16,397 ERROR [STDERR]     at org.jboss.ejb3.interceptors.container.ContainerMethodInvocationWrapper.invokeNext(ContainerMethodInvocationWrapper.java:72)
04:46:16,398 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.invoke(InterceptorSequencer.java:76)
04:46:16,399 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.InterceptorSequencer.aroundInvoke(InterceptorSequencer.java:62)
04:46:16,401 ERROR [STDERR]     at sun.reflect.GeneratedMethodAccessor950.invoke(Unknown Source)
04:46:16,402 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
04:46:16,403 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:597)
04:46:16,404 ERROR [STDERR]     at org.jboss.aop.advice.PerJoinpointAdvice.invoke(PerJoinpointAdvice.java:174)
04:46:16,405 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,406 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.fillMethod(InvocationContextInterceptor.java:74)
04:46:16,407 ERROR [STDERR]     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_fillMethod_446939511.invoke(InvocationContextInterceptor_z_fillMethod_446939511.java)
04:46:16,408 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,408 ERROR [STDERR]     at org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor.setup(InvocationContextInterceptor.java:90)
04:46:16,409 ERROR [STDERR]     at org.jboss.aop.advice.org.jboss.ejb3.interceptors.aop.InvocationContextInterceptor_z_setup_446939511.invoke(InvocationContextInterceptor_z_setup_446939511.java)
04:46:16,410 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,411 ERROR [STDERR]     at org.jboss.ejb3.async.impl.interceptor.AsynchronousServerInterceptor.invoke(AsynchronousServerInterceptor.java:128)
04:46:16,412 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,414 ERROR [STDERR]     at org.jboss.ejb3.connectionmanager.CachedConnectionInterceptor.invoke(CachedConnectionInterceptor.java:62)
04:46:16,415 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,416 ERROR [STDERR]     at org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor.invoke(TransactionScopedEntityManagerInterceptor.java:56)
04:46:16,417 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,418 ERROR [STDERR]     at org.jboss.ejb3.AllowedOperationsInterceptor.invoke(AllowedOperationsInterceptor.java:47)
04:46:16,419 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,420 ERROR [STDERR]     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
04:46:16,421 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,422 ERROR [STDERR]     at org.jboss.ejb3.stateless.StatelessInstanceInterceptor.invoke(StatelessInstanceInterceptor.java:68)
04:46:16,423 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,424 ERROR [STDERR]     at org.jboss.ejb3.core.context.SessionInvocationContextAdapter.proceed(SessionInvocationContextAdapter.java:95)
04:46:16,425 ERROR [STDERR]     at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invokeInOurTx(CMTTxInterceptor.java:247)
04:46:16,426 ERROR [STDERR]     at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.required(CMTTxInterceptor.java:349)
04:46:16,427 ERROR [STDERR]     at org.jboss.ejb3.tx2.impl.CMTTxInterceptor.invoke(CMTTxInterceptor.java:209)
04:46:16,428 ERROR [STDERR]     at org.jboss.ejb3.tx2.aop.CMTTxInterceptorWrapper.invoke(CMTTxInterceptorWrapper.java:52)
04:46:16,430 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,431 ERROR [STDERR]     at org.jboss.aspects.tx.TxPropagationInterceptor.invoke(TxPropagationInterceptor.java:76)
04:46:16,432 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,434 ERROR [STDERR]     at org.jboss.ejb3.tx.NullInterceptor.invoke(NullInterceptor.java:42)
04:46:16,435 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,436 ERROR [STDERR]     at org.jboss.ejb3.security.Ejb3AuthenticationInterceptorv2.invoke(Ejb3AuthenticationInterceptorv2.java:182)
04:46:16,436 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,437 ERROR [STDERR]     at org.jboss.ejb3.ENCPropagationInterceptor.invoke(ENCPropagationInterceptor.java:41)
04:46:16,438 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,439 ERROR [STDERR]     at org.jboss.ejb3.BlockContainerShutdownInterceptor.invoke(BlockContainerShutdownInterceptor.java:67)
04:46:16,440 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,441 ERROR [STDERR]     at org.jboss.ejb3.core.context.CurrentInvocationContextInterceptor.invoke(CurrentInvocationContextInterceptor.java:47)
04:46:16,442 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,443 ERROR [STDERR]     at org.jboss.aspects.currentinvocation.CurrentInvocationInterceptor.invoke(CurrentInvocationInterceptor.java:67)
04:46:16,444 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,446 ERROR [STDERR]     at org.jboss.ejb3.interceptor.EJB3TCCLInterceptor.invoke(EJB3TCCLInterceptor.java:86)
04:46:16,447 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,448 ERROR [STDERR]     at org.jboss.ejb3.stateless.StatelessContainer.dynamicInvoke(StatelessContainer.java:392)
04:46:16,449 ERROR [STDERR]     at org.jboss.ejb3.remoting.IsLocalInterceptor.invokeLocal(IsLocalInterceptor.java:88)
04:46:16,450 ERROR [STDERR]     at org.jboss.ejb3.remoting.IsLocalInterceptor.invoke(IsLocalInterceptor.java:75)
04:46:16,451 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,452 ERROR [STDERR]     at org.jboss.ejb3.async.impl.interceptor.AsynchronousClientInterceptor.invoke(AsynchronousClientInterceptor.java:143)
04:46:16,453 ERROR [STDERR]     at org.jboss.aop.joinpoint.MethodInvocation.invokeNext(MethodInvocation.java:102)
04:46:16,454 ERROR [STDERR]     at org.jboss.aspects.remoting.PojiProxy.invoke(PojiProxy.java:62)
04:46:16,455 ERROR [STDERR]     at $Proxy215.invoke(Unknown Source)
04:46:16,456 ERROR [STDERR]     at org.jboss.ejb3.proxy.impl.handler.session.SessionProxyInvocationHandlerBase.invoke(SessionProxyInvocationHandlerBase.java:185)
04:46:16,457 ERROR [STDERR]     at $Proxy224.addMyEntity(Unknown Source)
04:46:16,458 ERROR [STDERR]     at test.mycomp.services.MyServicesImpl.addMyEntity(MyServicesImpl.java:134)
04:46:16,459 ERROR [STDERR]     at tv.mycomp.controllers.MyController.addMyEntity(MyController.java:106)
04:46:16,460 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
04:46:16,461 ERROR [STDERR]     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
04:46:16,462 ERROR [STDERR]     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
04:46:16,463 ERROR [STDERR]     at java.lang.reflect.Method.invoke(Method.java:597)
04:46:16,464 ERROR [STDERR]     at org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:472)
04:46:16,465 ERROR [STDERR]     at org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:409)
04:46:16,466 ERROR [STDERR]     at org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
04:46:16,468 ERROR [STDERR]     at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
04:46:16,469 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:874)
04:46:16,470 ERROR [STDERR]     at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:808)
04:46:16,471 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
04:46:16,472 ERROR [STDERR]     at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:441)
04:46:16,473 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:754)
04:46:16,474 ERROR [STDERR]     at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
04:46:16,475 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:324)
04:46:16,476 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
04:46:16,478 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:274)
04:46:16,479 ERROR [STDERR]     at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:242)
04:46:16,480 ERROR [STDERR]     at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275)
04:46:16,481 ERROR [STDERR]     at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
04:46:16,482 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:181)
04:46:16,484 ERROR [STDERR]     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.event(CatalinaContext.java:285)
04:46:16,485 ERROR [STDERR]     at org.jboss.modcluster.catalina.CatalinaContext$RequestListenerValve.invoke(CatalinaContext.java:261)
04:46:16,486 ERROR [STDERR]     at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:88)
04:46:16,487 ERROR [STDERR]     at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:100)
04:46:16,488 ERROR [STDERR]     at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:159)
04:46:16,489 ERROR [STDERR]     at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
04:46:16,490 ERROR [STDERR]     at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
04:46:16,491 ERROR [STDERR]     at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
04:46:16,492 ERROR [STDERR]     at org.jboss.web.tomcat.service.request.ActiveRequestResponseCacheValve.invoke(ActiveRequestResponseCacheValve.java:53)
04:46:16,493 ERROR [STDERR]     at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:362)
04:46:16,494 ERROR [STDERR]     at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877)
04:46:16,495 ERROR [STDERR]     at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:654)
04:46:16,496 ERROR [STDERR]     at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:951)
04:46:16,498 ERROR [STDERR]     at java.lang.Thread.run(Thread.java:662)
04:46:16,501 ERROR [STDERR] Caused by: javax.transaction.RollbackException: ARJUNA-16083 Can't register synchronization because the transaction is in aborted state
04:46:16,502 ERROR [STDERR]     at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.registerSynchronizationImple(TransactionImple.java:363)
04:46:16,503 ERROR [STDERR]     at com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionImple.registerSynchronization(TransactionImple.java:344)
04:46:16,504 ERROR [STDERR]     at org.hibernate.transaction.JTATransaction.registerSynchronization(JTATransaction.java:313)
04:46:16,505 ERROR [STDERR]     ... 108 more

以下是persistence.xml中与事务相关的属性

<property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup" />

<property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.JTATransactionFactory" />

<property name="hibernate.transaction.flush_before_completion" value="true" />

我在任何地方都看到并阅读了关于在回滚事务后关闭实体管理器的信息。但为什么我会得到上述异常?是预期的还是我在这里遗漏了什么?

提前非常感谢,如果您需要任何有关相同的进一步信息,请告诉我。

4

1 回答 1

0

那将是一个错误。但是,我们不再支持 Hibernate 3.6。你能在 4.1 中重现它吗?堆栈跟踪与 4.1 不同...

于 2012-10-03T14:55:38.280 回答