How does transaction management work in Spring with @Transactional?
Learn how Spring @Transactional manages database transactions with AOP proxies, automatic commit and rollback, and why self-invocation bypasses it.
Expected Interview Answer
@Transactional tells Spring to wrap a method in a database transaction so that all its operations either commit together or roll back together, managed declaratively through an AOP proxy rather than manual begin/commit code.
When a bean with @Transactional is called, Spring's proxy starts a transaction before the method runs, binds the connection to the current thread, and either commits on normal return or rolls back on an unchecked exception. Because it works via proxies, self-invocation (calling another @Transactional method inside the same class) bypasses it. By default it rolls back on RuntimeException and Error, but not checked exceptions unless you configure rollbackFor.
- Declarative, no manual begin/commit/rollback code
- Automatic rollback on unchecked exceptions
- Consistent connection handling per thread
- Configurable propagation, isolation, and readOnly
- Keeps business logic free of boilerplate
AI Mentor Explanation
Think of an over in cricket as an all-or-nothing unit. The umpire only records the over once all six legal deliveries are complete; if the bowler is pulled mid-over for a serious breach, the partial deliveries are treated as void and the over is restarted cleanly. @Transactional works the same way: the whole method's changes count only when it finishes, and a failure rolls everything back to before it began.
Step-by-Step Explanation
Step 1
Annotate the method or class
Add @Transactional; Spring wraps the bean in an AOP proxy that intercepts calls.
Step 2
Transaction begins
Before the method body runs, the proxy asks the transaction manager to start or join a transaction and binds the connection to the thread.
Step 3
Run business logic
All repository and JDBC operations on that thread share the same transaction and connection.
Step 4
Commit on success
If the method returns normally, the proxy commits the transaction, flushing changes to the database.
Step 5
Rollback on failure
If an unchecked exception propagates out, the proxy rolls back; use rollbackFor to include checked exceptions.
What Interviewer Expects
- Knowing it is declarative AOP-proxy based
- Default rollback only on unchecked exceptions
- Awareness that self-invocation bypasses the proxy
- Understanding commit-on-return, rollback-on-exception
- Mentioning rollbackFor for checked exceptions
Common Mistakes
- Assuming checked exceptions trigger rollback by default
- Expecting @Transactional to work on private or self-invoked methods
- Putting @Transactional on a method not called through the proxy
- Catching an exception and swallowing it, preventing rollback
- Confusing flush with commit
Best Answer (HR Friendly)
“In Spring, @Transactional is a simple label you put on a method so the database treats all its work as a single unit. If everything succeeds the changes are saved together; if something goes wrong, Spring automatically undoes all of them so the data never ends up half-updated.”
Code Example
@Service
public class TransferService {
private final AccountRepository accounts;
public TransferService(AccountRepository accounts) {
this.accounts = accounts;
}
@Transactional(rollbackFor = InsufficientFundsException.class)
public void transfer(Long from, Long to, BigDecimal amount) {
accounts.debit(from, amount); // both succeed together
accounts.credit(to, amount); // or both roll back
}
}Follow-up Questions
- Why does self-invocation of a @Transactional method not start a transaction?
- How do you make a checked exception trigger rollback?
- What is the default propagation of @Transactional?
- What does readOnly = true actually do?
- How does Spring bind the connection to the current thread?
MCQ Practice
1. By default, @Transactional rolls back on which exceptions?
Spring rolls back on RuntimeException and Error by default; checked exceptions need rollbackFor.
2. Why can a self-invoked @Transactional method fail to start a transaction?
@Transactional works through an AOP proxy; calling the method from within the same bean skips the proxy.
3. What happens on normal return from a @Transactional method?
On a clean return the proxy commits, flushing all changes atomically to the database.
Flash Cards
How does @Transactional work under the hood? — An AOP proxy begins a transaction before the method and commits or rolls back after it.
What triggers rollback by default? — Unchecked exceptions (RuntimeException) and Error; checked exceptions need rollbackFor.
Why does self-invocation bypass it? — Internal calls skip the proxy, so the interceptor never runs.
What does readOnly=true hint? — It signals a read-only transaction, letting the provider optimize (e.g. skip dirty checking).
Continue Learning
Related Interview Questions
What is the propagation and isolation level of a Spring transaction?
hard
What is the difference between JpaRepository, CrudRepository, and PagingAndSortingRepository?
medium
What are Spring bean scopes and how do singleton and prototype differ?
medium
What is the Spring bean lifecycle and what callbacks does it provide?
medium