One way to configure Spring to rollback on a non RuntimeExceptions
is using @Transactional(rollbackFor=...)
annotation on the service classes. The problem with this approach is that we need to define (rollbackFor=...) for almost all the service classes which seems really redundant.
My question: Is there any way to configure a default behaviour for Spring transaction manager to rollback on a non RuntimeException
whenever it happens without declaring it on every @Transactional
annotation. Something like using @ApplicationException(rollback=true)
annotation on an exception class in EJB.
You can't do it for application level with @Transactional , but you can :
variant 1 : extend @Transactional annotation and put it as default value for rollbackfor. But set rollbackFor unchecked exceptions only that you need .With this you can control rollbacks only for case that you sure , and avoid copy past of @Transactional(rollbackFor =MyCheckedException.class)
Like:
And use this annotation instead of standard @Transactional.
variant 2 : you can create extension from AnnotationTransactionAttributeSource and override method determineTransactionAttribute:
TransactionAttribute see TransactionAttribute api , there is a method
}
Second approach is not so good as first as you do it really global for transaction manager. Better use custom annotation as you can control it any apply only for methods/classes where you really need it. But if you need it in any case use second variant , it will be your default transnational behavior.
This config solves it: