Tuesday 27 March 2012

Transaction marked as rollbackOnly

This exception is exceptionally frustrating: 

org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:476)
    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 $Proxy75.call(Unknown Source)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:207)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    at java.lang.Thread.run(Thread.java:662)
Caused by: javax.persistence.RollbackException: Transaction marked as rollbackOnly
    at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:73)
    at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:467)
    ... 12 more


 ....of course you want to know what set the rollbackOnly flag and why.  Here's one way of finding out:
  1. Attach to application with debugger (e.g. Debug Tomcat)
  2. Assuming Hibernate is the underlying implementation, break on the setRollbackOnly() method in org.hibernate.ejb.TransactionImpl (Eclipse tip: use Ctrl+Shift+T to search for a class)
  3. when the debugger breaks on this method you can bubble up the call stack in the debugger and usually find out what caused this transaction to be marked as rollbackOnly.
Thanks to Useful Code Snippets blog:  But why was my transaction marked as rollbackOnly?

1 comment:

  1. Thanks a lot your posting did help me to find out the root cause of this rollbackOnly error.

    Thanks again for sharing

    ReplyDelete