Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practices for rolling back transactions in Spring 3/Hibernate

Referencing Spring documentation:

Any RuntimeException will trigger rollback, and any checked Exception will not

Referencing javapractices.com

Unchecked exceptions :

  • represent defects in the program (bugs) - often invalid arguments passed to a non-private method. To quote from The Java Programming Language, by Gosling, Arnold, and Holmes : "Unchecked runtime exceptions represent conditions that, generally speaking, reflect errors in your program's logic and cannot be reasonably recovered from at run time."
  • are subclasses of RuntimeException, and are usually implemented using IllegalArgumentException, NullPointerException, or IllegalStateException
  • a method is not obliged to establish a policy for the unchecked exceptions thrown by its implementation (and they almost always do not do so)

Checked exceptions :

  • represent invalid conditions in areas outside the immediate control of the program (invalid user input, database problems, network outages, absent files)
  • are subclasses of Exception
  • a method is obliged to establish a policy for all checked exceptions thrown by its implementation (either pass the checked exception further up the stack, or handle it somehow)

If during my business logic I discover a problem and I want to rollback the changes, I have to throw a new RuntimeException? It's not really a RuntimeException (unchecked exception) since I've identified it in the logic. Or perhaps I'm misunderstanding these concepts?

My real question, what's best practices for rolling back a transaction in my @Transactional service methods?

like image 402
Corey Avatar asked Dec 09 '10 19:12

Corey


People also ask

Does @transactional rollback?

The @Transactional annotation makes use of the attributes rollbackFor or rollbackForClassName to rollback the transactions, and the attributes noRollbackFor or noRollbackForClassName to avoid rollback on listed exceptions. The default rollback behavior in the declarative approach will rollback on runtime exceptions.

What types of exception does Spring automatically rollback a transaction?

The Spring documentation says: While the EJB default behavior is for the EJB container to automatically roll back the transaction on a system exception (usually a runtime exception), EJB CMT does not roll back the transaction automatically on an application exception (that is, a checked exception other than java.

How do you handle rollback in JPA?

To rollback a transaction you can use @Transaction annotation. You can either implement it on method level or class level. Class level @Transactional(rollbackFor = Exception.


1 Answers

If you're using checked exceptions you simply add them to the rollbackFor property of your @Transactional annotation.

@Transactional(rollbackFor = { MyInvalidUserException.class, MyApplicationException.class })
public void method() throws MyInvalidUserException,  MyApplicationException { 
    ... 
    ... 
}

etc.

org.life.java's answer also works fine. It's an academic decision if you want to intermix programmatic transaction management into your declarative transactions or keep it strictly declarative.

like image 142
Affe Avatar answered Sep 19 '22 20:09

Affe