Java Developer interview question
How does Spring @Transactional work?
@Transactional is normally applied by a Spring proxy that opens, commits, or rolls back a transaction around an intercepted public call; self-invocation bypasses the proxy and rollback defaults matter.
Direct answer
What a strong answer covers
How it works
Spring wraps a bean that has @Transactional methods in a proxy. The proxy intercepts external calls, starts or joins a transaction according to the propagation setting, binds the connection to the thread, and commits when the method returns or rolls back when a RuntimeException escapes; checked exceptions do not trigger rollback unless rollbackFor says so. Because the interception lives in the proxy, a method calling another @Transactional method on this bypasses it entirely, and private or final methods cannot be intercepted at all.
Example
A service method annotated @Transactional that calls this.saveAudit(), also annotated with REQUIRES_NEW, runs the audit inside the outer transaction and loses it on rollback because the self-call never passed through the proxy. Moving saveAudit into a separate bean restores the intended independent transaction.
Common mistakes
- Annotating an interface method and expecting it to apply with class-based proxies.
- Catching an exception inside the transactional method and expecting the transaction to roll back anyway.
- Holding a transaction open across a remote HTTP call and exhausting the connection pool under load.
A useful answer structure
Answer from your own work.
Do not memorise a script: use only examples you can explain honestly in follow-up questions.
Open the desktop workspace and rehearse a clear answer in your own words.