Scrivio Interview library
InternalsFoundations6 min read · updated 12 November 2026

How does @Transactional work in Spring? Describe what happens from the moment a caller invokes an annotated method on a bean.

Spring wraps the bean in a proxy, and the caller actually calls the proxy. The proxy's TransactionInterceptor reads the method's transaction attributes, asks the PlatformTransactionManager to begin or join a transaction, and the manager binds the connection or EntityManager to the current thread through TransactionSynchronizationManager. When the method returns, the interceptor commits; when it throws an exception that matches the rollback rules, it rolls back. Only calls that go through the proxy get any of this.

Why this question is asked

@Transactional looks like magic: one annotation and our repository calls suddenly share a database connection and commit together. Understanding what really happens explains a whole family of bugs, from self-invocation that skips the transaction to repositories that see different connections in a new thread. Interviewers ask this to find out whether we can reason about those bugs instead of guessing. In this answer, we will follow one call from the caller to the database and back: how the proxy is created at startup, what the interceptor does, how the transaction manager ties resources to the thread, how commit and rollback are decided, and why only proxied calls count.

1Spring Creates a Proxy at Startup

When the application context starts, Spring looks at every bean for transactional annotations. For each bean that has them, an auto-proxy creator, a post-processor that runs after the bean is created, wraps it in a proxy: an object generated at runtime that looks like our bean from the outside and forwards calls to it.

There are two kinds. A JDK dynamic proxy implements the bean's interfaces and can only be used through them. A CGLIB proxy is a generated subclass of our class, so it can be injected by the class type too. Spring Boot uses CGLIB class-based proxies by default (spring.aop.proxy-target-class=true). Either way, what other beans receive by injection is the proxy, not our object.

Java
@Service
class OrderService {
@Transactional
public void placeOrder(Order o) { repository.save(o); }
}
System.out.println(orderService.getClass().getName());
// com.example.OrderService$$SpringCGLIB$$0 (a subclass generated at startup)
System.out.println(AopUtils.isAopProxy(orderService)); // true

2The Interceptor Reads the Transaction Attributes

When a controller calls orderService.placeOrder(o), the call lands on the proxy. The proxy holds a chain of advice, pieces of code that run around the method, and for transactions that advice is TransactionInterceptor.

The interceptor first asks a TransactionAttributeSource what the method needs. It finds the @Transactional on the method, or on the class if the method has none, and turns it into a TransactionAttribute: propagation, isolation, timeout, read-only flag and rollback rules. These are cached per method, so the annotation is not parsed on every call. If the method has no attribute, the interceptor just calls through.

3The Transaction Manager Begins or Joins

Next the interceptor calls getTransaction() on a PlatformTransactionManager, the component that knows how to talk to one kind of resource. With Spring Data JPA that is JpaTransactionManager; with plain JDBC it is DataSourceTransactionManager.

The manager first checks whether a transaction is already running on this thread. If it is, the propagation setting decides: REQUIRED joins it, REQUIRES_NEW suspends it and starts another, and so on. If not, it starts one: it takes a connection from the pool, sets autoCommit to false, applies the isolation level and read-only flag, and with JPA creates an EntityManager for the transaction.

Then comes the step that ties everything together. The manager binds these resources to the current thread with TransactionSynchronizationManager, which stores them in ThreadLocal variables keyed by the DataSource or EntityManagerFactory. When our repository later needs an EntityManager, Spring's shared EntityManager proxy looks in that ThreadLocal and finds the transaction's one. So every repository call inside placeOrder() uses the same connection, without us passing it around.

Java
@Transactional
public void placeOrder(Order o) {
log.info("active: {}", TransactionSynchronizationManager.isActualTransactionActive()); // active: true
log.info("name: {}", TransactionSynchronizationManager.getCurrentTransactionName());
// name: com.example.OrderService.placeOrder
repository.save(o); // uses the EntityManager bound to this thread
}
Step throughOne call to placeOrder()
CallerProxy + interceptorTransaction managerOrderServiceDatabase
placeOrder(o)
getTransaction(attributes)
take connection, autoCommit false
bind resources to this thread
placeOrder(o) on the real object
insert via the thread-bound connection
returns normally
commit(status)
flush, commit, release connection
returns

10 messages in order. Play them, or step through them one at a time.

The real object never touches the transaction; the proxy does all of it around the call.

Notice what this means for threads: a new thread, an @Async method or a parallel stream has empty ThreadLocals, so it does not see the transaction at all.

4Commit or Roll Back

The interceptor wraps the call to our method in a try block. If the method returns normally, it calls commit() on the manager. With JPA, commit first flushes the persistence context, which sends the pending INSERT and UPDATE statements, then commits the JDBC connection, unbinds the resources from the thread and returns the connection to the pool.

If the method throws, the interceptor asks the attribute whether this exception should cause a rollback. By default that is true for RuntimeException and Error and false for checked exceptions. On a match it calls rollback(); otherwise it commits anyway. Then it rethrows the exception to the caller, so the caller still sees the failure.

One more detail: if our method only joined an outer transaction, commit and rollback from the inner proxy do not end anything. A commit does nothing, and a rollback marks the shared transaction rollback-only, which is why an outer method that catches an inner exception can still fail later with UnexpectedRollbackException.

5Why Only Proxied Calls Are Transactional

All of this happens in the proxy. Our real object has no idea transactions exist. So a call that does not pass through the proxy gets none of it. The two common cases are a call from one method to another inside the same class, which is a call on this, and a call to a private or final method, which the proxy cannot override.

Java
@Service
class OrderService {
public void placeAll(List<Order> orders) {
orders.forEach(this::placeOrder); // this.placeOrder(): skips the proxy
}
@Transactional
public void placeOrder(Order o) { ... } // active: false when called from placeAll()
}

The fix is to call through a Spring bean: move the method into another bean, use TransactionTemplate for explicit boundaries, or, with more setup, use AspectJ weaving, which changes the bytecode instead of relying on a proxy.

6Follow-up questions

What is the difference between a logical and a physical transaction?

A physical transaction is the real one on the database connection. A logical transaction is the scope of one @Transactional method. With REQUIRED, several logical transactions share one physical transaction, and only the outermost one actually commits or rolls back.

What happens if the method starts a new thread?

The new thread has its own ThreadLocal storage, so TransactionSynchronizationManager finds nothing bound there. Repository calls in that thread run in their own transactions or with autocommit, and they do not see uncommitted changes from the original transaction.

Can you use transactions without the annotation?

Yes. TransactionTemplate calls the same PlatformTransactionManager directly in code: we pass it a callback and it begins, commits or rolls back around it. It is useful when the boundary must be inside a method, such as one transaction per item in a loop.

7Conclusion

In this answer, we followed one call through @Transactional. At startup Spring wraps the bean in a proxy. On each call, TransactionInterceptor reads the cached transaction attributes and asks the PlatformTransactionManager to begin or join a transaction, which binds the connection and EntityManager to the thread through TransactionSynchronizationManager so every repository call shares them. After the method, the interceptor commits, or rolls back when the exception matches the rollback rules. Because all of this lives in the proxy, only calls that go through the proxy are transactional.

What to say in the interviewAbout 80 seconds aloud

Open with

At startup Spring sees the annotation and wraps the bean in a proxy. In Spring Boot that is a CGLIB subclass by default, otherwise it can be a JDK dynamic proxy on the interfaces.

Then, if they want more

Other beans get the proxy injected. When a caller invokes the method, the proxy runs TransactionInterceptor. It looks up the transaction attributes for that method, propagation, isolation, timeout, read-only and rollback rules, which are cached, and calls getTransaction on the PlatformTransactionManager, for example JpaTransactionManager. The manager checks whether a transaction is already active on the thread.

If not, it gets a connection, turns off autocommit, applies isolation and read-only, creates the EntityManager, and binds them to the current thread through TransactionSynchronizationManager, which is ThreadLocal-based. That is how every repository call in the method uses the same connection. Then the real method runs. If it returns normally, the interceptor commits, which with JPA flushes first.

If it throws, the interceptor checks the rollback rules, by default RuntimeException and Error, rolls back or commits, and rethrows. If the method only joined an outer transaction, a failure marks it rollback-only instead. The important consequence is that only calls through the proxy are transactional, so self-invocation, private methods and new threads do not get the transaction.

About this answer

Last updated
12 November 2026
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "How does @Transactional work in Spring? Describe what happens from the moment a caller...": https://scrivio-interview-library.netlify.app/databases/transactions/132acc6abc/

More in Transactions