Spring transaction: rollback on Exception or Throwable

As I understand catching Error will help us behave correctly even when something really bad happen. Or maybe it wouldn’t help?

You don’t need to explicitly specify rollbackFor = Throwable.class, because spring will by default rollback the transaction if an Error occurs.

See 1.4.3. Rolling Back a Declarative Transaction

In its default configuration, the Spring Framework’s transaction infrastructure code marks a transaction for rollback only in the case of runtime, unchecked exceptions. That is, when the thrown exception is an instance or subclass of RuntimeException. (Error instances also, by default, result in a rollback). Checked exceptions that are thrown from a transactional method do not result in rollback in the default configuration.

Or take a look at the DefaultTransactionAttribute

public boolean rollbackOn(Throwable ex) {
    return (ex instanceof RuntimeException || ex instanceof Error);
}

Leave a Comment