0 day streak
java / intermediate
Snippet

Transaction Propagation: REQUIRES_NEW

Propagation.REQUIRES_NEW creates a separate transaction that is independent of the caller's transaction. If the caller's transaction rolls back, this one will still commit, which is ideal for audit logs.

snippet.java
java
1
2
3
4
5
6
7
@Service
public class AuditService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void logAction(String action) {
auditRepository.save(new AuditLog(action));
}
}
spring
Breakdown
1
propagation = Propagation.REQUIRES_NEW
Suspends the current transaction and starts a fresh one for this method.