I knew the basics: Spring only rolls back on RuntimeException and Error by default, checked exceptions slip through unless you explicitly add them via rollbackFor.
Start by explaining the default rollback behavior (only unchecked exceptions) and how rollbackFor/noRollbackFor override it. Then describe the interaction when both are set, emphasizing that noRollbackFor takes precedence. Finally, highlight common pitfalls such as self-invocation, checked exceptions, and nested transactions.
Pro tip: Mention that rollbackFor and noRollbackFor are evaluated at the method level, and if both are specified, noRollbackFor wins—this shows deep understanding. Also, note that these attributes only apply to the outermost transaction when using propagation REQUIRED, which is a common gotcha.
By default, Spring rolls back only on unchecked exceptions (RuntimeException and Error). Checked exceptions do not trigger a rollback unless explicitly configured.
rollbackFor specifies exceptions that should trigger a rollback, while noRollbackFor specifies exceptions that should not trigger a rollback. Both accept an array of Class objects.
If both are set, noRollbackFor takes precedence over rollbackFor. So if an exception matches both, no rollback occurs.
Pitfalls include: self-invocation bypassing proxy, checked exceptions not rolling back by default, rollback rules only applying to the outermost transaction, and misunderstanding propagation effects.
AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.