RSS

But why was my transaction marked as rollbackOnly ?

18 May

While developing with the JPA and Spring’s org.springframework.orm.jpa.JpaTransactionManager and, in this case, Hibernate…it’s not unusual to run into exceptions like this:
javax.persistence.RollbackException: Transaction marked as rollbackOnly
..and the common frustration is that you can see exactly what has happened but you don’t necessarily know why.

Of course there’s no blanket answer for why – so that is not what this post will provide.
Nope.
All I can do is share what I do to find out.

I head into debug mode, and I put a breakpoint on the setRollbackOnly() method of whichever implementation of javax.persistence.EntityTransaction is in play. If you’re using Hibernate 3 as I am, then this is org.hibernate.ejb.TransactionImpl.

And if you aren’t sure or can’t readily work out what the underlying transaction class is, then you can step back a little to the org.springframework.orm.jpa.JpaTransactionManager and put your breakpoint on the last line of this method and step in from there.

protected void doSetRollbackOnly(DefaultTransactionStatus status) {
 		..
		txObject.setRollbackOnly();
}

This will help you work out what the underlying transaction implementation is. It might appear to also help work out what the cause of the rollback was, but I find it’s better to keep an eye on the transaction implementation itself as the JpaTransactionManager method isn’t always called when the rollback is set.

Hope this info helps. It’s something I use occasionally and don’t always remember it next time I need it. So I’ve written it down here.

 
3 Comments

Posted by on May 18, 2011 in Hibernate, JPA, Spring

 

3 responses to “But why was my transaction marked as rollbackOnly ?

  1. trein

    March 31, 2013 at 12:43 pm

    Thanks. I really appreciated your post. Following your tips I could identify what was causing the rollback.

     
  2. Christine (@Xtien)

    March 20, 2014 at 6:43 am

    +1

     
  3. Frankie

    June 4, 2014 at 1:07 pm

    Your post really give me a good way to trace the problem. Thanks a lot.

     

Leave a comment