Sub topic 16
Case study: @Transactional as proxy advice
This chapter assumes you know Spring beans, the proxy pattern, and how around advice works. @Transactional is the most common use of Spring AOP. It is ordinary around advice applied through a proxy, so the limits of proxies apply to it.
What the proxy does
When a bean has @Transactional methods, Spring wraps it in a proxy. A transaction interceptor runs inside that proxy. For each call it does roughly this:
- Ask the transaction manager to begin a transaction, or join an existing one.
- Call the real method with
proceed(). - Commit if the method returns normally.
- Roll back if it throws a rollback-triggering exception.
The target class contains no transaction code. All of it lives in the interceptor.
Rollback rules
By default the interceptor rolls back on unchecked exceptions (RuntimeException and Error). A checked exception commits the transaction unless you ask otherwise.
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class PaymentService {
@Transactional(rollbackFor = Exception.class)
public void pay(String account) throws Exception {
// a checked exception thrown here now triggers rollback
}
}Where proxy limits show up
The limits from earlier chapters apply directly.
| Situation | Effect on @Transactional |
|---|---|
| Call from another bean | Works, because the call goes through the proxy. |
| Self-invocation (`this.method()`) | Ignored, because the call skips the proxy. |
| `private` method | Ignored, because proxies cannot intercept it. |
| `final` method on a CGLIB proxy | Not intercepted, so it runs with no transaction. |
| Object created with `new` | Ignored, because it is not a Spring bean. |
Consider the ReportService example from the self-invocation chapter. saveAll is called through the proxy, but it calls saveReport on this. The REQUIRES_NEW setting on saveReport never applies. Any transaction in effect belongs to the outer call, or there is none.
A common fix is to move saveReport into a second bean and inject it:
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@Service
class ReportWriter {
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void saveReport(String name) {
// now runs in its own transaction
}
}
@Service
public class ReportService {
private final ReportWriter writer;
public ReportService(ReportWriter writer) {
this.writer = writer;
}
public void saveAll(java.util.List<String> names) {
for (String name : names) {
writer.saveReport(name); // goes through the proxy
}
}
}Why a swallowed exception still commits
The interceptor only sees what leaves the method. If your code catches an exception and does not rethrow it, the proxy sees a normal return and commits. To roll back without throwing, call TransactionAspectSupport.currentTransactionStatus().setRollbackOnly().
Exercise
A @Service method process() has no annotation and calls this.update(), which is @Transactional(propagation = Propagation.REQUIRES_NEW). process() is called from a controller. Does update() run in a new transaction? Why or why not?
Show answer
No. The controller's call reaches the proxy, but process() then calls update() on this, which is the target object and not the proxy. The transaction interceptor never runs for update(), so no new transaction starts. Move update() to another bean and inject it.
Exercise
A @Transactional method catches a RuntimeException in a try/catch block and logs it without rethrowing. Is the transaction rolled back?
Show answer
No. The exception never leaves the method, so the interceptor sees a normal return and commits. Rethrow the exception, or call setRollbackOnly() on the current transaction status.