@Transactional: Propagation, Isolation and Rollback Rules
@Transactional is simple to add and easy to get wrong. Interviewers ask about propagation types like REQUIRED and REQUIRES_NEW, isolation levels, which exceptions roll back, readOnly, and why it sometimes silently does nothing.
What you will learn
- Explain the propagation types
- Choose isolation levels
- Know the default rollback rules
- List the reasons @Transactional is ignored
Key concepts
Propagation
REQUIRED (default: join the current transaction or start one), REQUIRES_NEW (suspend the current one and start a fresh one), NESTED (savepoint inside the current one), SUPPORTS, MANDATORY, NOT_SUPPORTED, NEVER.
Rollback rules
By default only unchecked exceptions (RuntimeException, Error) roll back. Checked exceptions commit unless you set rollbackFor = Exception.class.
readOnly = true
A hint: Hibernate skips dirty checking and flushes, and the driver or router may send queries to a replica. Good default for query methods.
Isolation
DEFAULT (database default), READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE. Raise it only where a race matters, or use locking.
Step by step
REQUIRES_NEW for an audit log that must survive
@Service
class PaymentService {
private final AuditService audit;
@Transactional
public void pay(Order o) {
audit.record("payment attempt " + o.getId()); // separate transaction, commits even if pay fails
gateway.charge(o);
o.markPaid();
}
}
@Service
class AuditService {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void record(String msg) { auditRepository.save(new AuditLog(msg)); }
}Why @Transactional did nothing
- Self-invocation (this.method()) skips the proxy
- The method is private or final
- The class is not a Spring bean (created with new)
- A checked exception was thrown (commits by default)
- You caught the exception inside the method
- Wrong transaction manager when there are several datasources
Examples
Pessimistic lock in a repository
public interface AccountRepository extends JpaRepository<Account, Long> {
@Lock(LockModeType.PESSIMISTIC_WRITE)
@Query("select a from Account a where a.id = :id")
Optional<Account> findForUpdate(Long id); // SELECT ... FOR UPDATE
}Practise it
Try these without looking back. If you can explain each one out loud, you are ready for this question.
- Show that a checked exception does not roll back by default, then fix it.
- Explain "Transaction silently rolled back because it has been marked as rollback-only".
Interview questions
What interviewers ask next on this topic. Answer out loud first, then open the model answer.
Where should @Transactional go: controller, service or repository?
On the service layer, around one business use case, so all repository calls in it share one transaction. Controllers should not hold transactions, and Spring Data repository methods are already transactional individually.
What is UnexpectedRollbackException?
An inner REQUIRED method threw, which marked the shared transaction rollback-only; the outer method caught the exception and tried to commit. Spring rolls back and throws UnexpectedRollbackException. Use REQUIRES_NEW for the inner call if it must be independent, or let the exception propagate.
Summary
Put @Transactional on service methods, know REQUIRED vs REQUIRES_NEW, remember checked exceptions commit by default, use readOnly for queries, and avoid self-invocation and private methods.
Stuck on this one? Talk to Sumit.
Keys: F focus mode · ← → previous / next topic · Esc leave focus