Transaction Pitfalls (Part 3)
1. Incorrect rollbackFor Configuration
@Transactional by default only triggers rollback for RuntimeException and Error.
Problem:
- If
rollbackFor = ResultException.classis specified - When
ParamExceptionor other exceptions are thrown, transaction will not roll back
Solution:
- Use
rollbackFor = Exception.classor all exception types
2. Transaction Annotation Override
When subclass overrides parent class method with @Transactional, if transaction annotation is not explicitly declared, Spring AOP proxy may bypass transaction enhancement.
Solution:
- Defining methods in interfaces and implementing them is a more reliable approach
3. Nested Transaction Issues
Default propagation behavior is REQUIRED, sub-method joins parent transaction:
- Sub-method exception will cause entire transaction to roll back
- Even if parent method’s subsequent logic should have succeeded
Solution:
- Use
REQUIRES_NEW - Or catch internal exceptions