Card 05/ 07
All 7 cards
ComparisonDifficulty: Advanced1 min
REQUIRES_NEW vs the Default
bookRideThenFail() calls logIndependently(), then throws. logIndependently is REQUIRES_NEW — and its write survives the caller's rollback entirely.
@Transactional(propagation = Propagation.REQUIRES_NEW)
void logIndependently(String note) {
repo.save(new AuditEntity(note));
}
@Transactional
void bookRideThenFail() {
auditService.logIndependently("attempted booking");
throw new RuntimeException("booking failed after logging");
}REQUIRES_NEW audit log survived caller's rollback: count = 1| Propagation | When called inside an existing transaction |
|---|---|
REQUIRED (the default) | Joins it — one transaction, one outcome for both |
REQUIRES_NEW | Suspends the caller's transaction and starts a completely independent one, which commits or rolls back on its own |
REQUIRES_NEW is the right call for exactly this shape: an audit entry, a notification, anything that should be recorded regardless of whether the operation it's logging ultimately succeeds. The default, REQUIRED, is right whenever the two pieces of work should genuinely live or die together.