AnsweredAssumed Answered

a strange question about jta transaction

Question asked by jonlee on Jun 24, 2014
Latest reply on Jul 4, 2014 by jbarrez
I use JTA manage my transaction, and from spring my service bean like this:

@Transactional
@Component("myService")
public class MyServiceImpl implements MyService {
   
   @PersistenceContext(unitName="platform")
   private EntityManager em;
   
   @Autowired
   private TaskService taskService;
   

   @Override
   public void fooSelf(String processInstanceId, MyDomain myDomain) throws RuntimeException{
      List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
      for (Task task : tasks) {
         taskService.complete(task.getId());
      }
      em.merge(myDomain);
   }

}
<code>

my unit test like this:
<code>
@Test
   public void testDm() throws Exception{
      MyDomain myDomain = new MyDomain();
      myDomain.setId("1");
      myDomain.setName("a");
      String processInstanceId = testStartProcess();
      myService.fooSelf(processInstanceId, myDomain);
   }
   
   private String testStartProcess() throws Exception{
      return runtimeService.startProcessInstanceByKey("robbie", null, null).getId();
   }


It looks very simple. if I use mysql, oracle, sqlserver, everything is ok.  but when I use h2 as the database, something is wrong.
the exception stack below:

org.springframework.transaction.UnexpectedRollbackException: JTA transaction unexpectedly rolled back (maybe due to a timeout); nested exception is javax.transaction.RollbackException: The transaction was set to rollback only
   at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1013)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)
   at org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)
   at org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)
   at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)
   at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
   at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
   at com.sun.proxy.$Proxy24.fooSelf(Unknown Source)
   at com.bjhy.test.JunitTest.testDm(JunitTest.java:59)
   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.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
   at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
   at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
   at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
   at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
   at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
   at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
   at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
   at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
   at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
   at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
   at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
   at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
   at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
   at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
   at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
   at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
   at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
   at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
   at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: javax.transaction.RollbackException: The transaction was set to rollback only
   at com.atomikos.icatch.jta.TransactionImp.rethrowAsJtaRollbackException(TransactionImp.java:68)
   at com.atomikos.icatch.jta.TransactionImp.commit(TransactionImp.java:222)
   at com.atomikos.icatch.jta.TransactionManagerImp.commit(TransactionManagerImp.java:501)
   at com.atomikos.icatch.jta.UserTransactionImp.commit(UserTransactionImp.java:130)
   at org.springframework.transaction.jta.JtaTransactionManager.doCommit(JtaTransactionManager.java:1010)
   … 36 more
Caused by: com.atomikos.icatch.RollbackException: The transaction was set to rollback only
   at com.atomikos.icatch.imp.TransactionStateHandler.commit(TransactionStateHandler.java:254)
   at com.atomikos.icatch.imp.CompositeTransactionImp.doCommit(CompositeTransactionImp.java:287)
   at com.atomikos.icatch.imp.CompositeTerminatorImp.commit(CompositeTerminatorImp.java:77)
   at com.atomikos.icatch.imp.CompositeTransactionImp.commit(CompositeTransactionImp.java:336)
   at com.atomikos.icatch.jta.TransactionImp.commit(TransactionImp.java:208)
   … 39 more


I don't know why. and the strange thing is if I change the location of the code, it will be ok in H2 database.just like this:

@Transactional
@Component("myService")
public class MyServiceImpl implements MyService {
   
   @PersistenceContext(unitName="platform")
   private EntityManager em;
   
   @Autowired
   private TaskService taskService;
   

   @Override
   public void fooSelf(String processInstanceId, MyDomain myDomain) throws RuntimeException{
      <blockcode>em.merge(myDomain);//first invoke merge method</blockcode>
      List<Task> tasks = taskService.createTaskQuery().processInstanceId(processInstanceId).list();
      for (Task task : tasks) {
         taskService.complete(task.getId());
      }
   }

}


Outcomes