Transaction Rollback Management

I have a problem with @Transactional annotation.

I have a method that does some things, and inside I throw and break an IllegalArgumentException .

I think (even if I caught the exception) that it sets the transaction as rollbackOnly (some sort of trigger for throwing an exception), and it ends without committing the transaction.

Here is the error:

 org.springframework.transaction.TransactionSystemException : Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Transaction marked as rollbackOnly 

I could add @Transactional no-rollback - for the exception that I picked and caught, but I don't think this is a real solution.

Maybe there is a way to rollback rollbackOnly on a transaction, but I don't think it's best practice too ...

So, you know how to do this?

Thanks,

+8
java spring hibernate jpa
source share
2 answers
  • Trying / catching with an IllegalArgumentException sounds like a code smell (Effective Java Element 57: Use Exceptions for Exceptional Conditions Only )

  • Regardless of whether the transaction is set to rollbackOnly , it depends on the proxy mechanism used. If you use the JDK proxy, the handler is outside and does not have the ability to register an excluded caught in a method call. If you use mode=aspectj , everything will be different. Also, if you have nested transactional contexts, you will have to use @Transactional(noRollbackFor=IllegalArgumentException.class) for the internal method.

+6
source share

If you cannot complete the transaction, you probably have an exception in your code. By attempting / tricking, you hide the complete exception and simply get a subtle general explanation. You will also get a rollback.

To understand your error and get a full description of your error, try dropping the try / catch file and letting the code explode. There you will see the real source of your problem.

+2
source share

All Articles