Transaction Pitfalls (Part 3)

1. Incorrect rollbackFor Configuration

@Transactional by default only triggers rollback for RuntimeException and Error.

Problem:

  • If rollbackFor = ResultException.class is specified
  • When ParamException or other exceptions are thrown, transaction will not roll back

Solution:

  • Use rollbackFor = Exception.class or 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