0 day streak
java / intermediate
Snippet

Fine-grained Transaction Rollback Strategies

You can customize transaction behavior by defining which exceptions should trigger a rollback and which should allow the transaction to commit anyway.

snippet.java
java
1
2
3
4
5
6
7
8
9
@Service
public class OrderService {
@Transactional(rollbackFor = PaymentException.class, noRollbackFor = LogException.class)
public void completeOrder(Order order) throws PaymentException {
inventoryService.update(order);
paymentService.charge(order);
logService.audit(order);
}
}
spring
Breakdown
1
rollbackFor = PaymentException.class
Ensures the transaction rolls back if a PaymentException occurs.
2
noRollbackFor = LogException.class
Allows the transaction to commit even if a non-critical LogException is thrown.