The @Transactional annotation tells Spring to run a method, or every public method of a class, inside a database transaction. Spring starts the transaction before the method, commits it when the method returns normally and rolls it back when the method throws a runtime exception, so you do not write begin, commit and rollback code yourself.
It is the standard way to keep a group of database operations atomic in Spring and Spring Boot applications.
Spring implements @Transactional with a proxy. When another bean calls your service, the call goes through a proxy that asks a PlatformTransactionManager to open a transaction, runs your method, and then commits or rolls back. Spring Boot registers the transaction manager and enables annotation-driven transactions for you, so you only add the annotation. Use the Spring annotation, org.springframework.transaction.annotation.Transactional, because it supports all the attributes below.
Because it is proxy based, two limits follow:
@Transactional settings are ignored. This is called the self-invocation problem.private methods never do.If withdraw throws, nothing is written. Either both balances change or neither does.
By default Spring rolls back on RuntimeException and Error. It does not roll back on checked exceptions, because Spring follows the EJB convention that a checked exception is a recoverable business condition. Change this with attributes:
Two traps are common. If you catch an exception inside the method and do not rethrow it, Spring sees a normal return and commits. And an exception thrown inside an inner @Transactional(propagation = REQUIRED) method marks the whole shared transaction as rollback-only, even if the outer method catches it, which leads to an UnexpectedRollbackException at commit.
| Attribute | Purpose | Common values |
|---|---|---|
propagation | What to do when a transaction already exists | REQUIRED (default), REQUIRES_NEW, SUPPORTS, MANDATORY, NEVER, NESTED |
isolation | How much concurrent transactions can see of each other | DEFAULT, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE |
readOnly | Hint that the method does not write | true or false |
timeout | Seconds before the transaction is marked for rollback | for example 5 |
REQUIRED joins an existing transaction or starts a new one. REQUIRES_NEW suspends the current transaction and starts an independent one, which suits audit logging that must survive a rollback of the main work.
readOnly = true lets the persistence provider and some drivers skip work such as dirty checking. It is a hint, not a guarantee that writes are blocked.
Put @Transactional on a class to apply it to all its public methods, and override it on single methods. Prefer placing it on the service layer, where a use case begins and ends, rather than on repositories or controllers. A controller transaction would also keep a database connection open while the response is rendered.
In tests, @Transactional on a Spring test class rolls the test's transaction back after each test by default. That helps keep tests isolated, but it can hide problems that appear only on commit, such as constraint violations.
The usual causes are a checked exception without rollbackFor, an exception caught inside the method, a call from within the same class, or a method that the proxy cannot intercept.
Both work in Spring, but jakarta.transaction.Transactional has fewer options. The Spring annotation supports readOnly, timeout and custom isolation, so most Spring projects use it.
The transaction is bound to the current thread. A method started with @Async runs in a different thread, so it does not share the caller's transaction and needs its own.
It starts a transaction, and locks depend on your isolation level and queries. The annotation itself takes no explicit lock.
@Transactional gives you declarative transactions through a proxy: commit on success and rollback on runtime exceptions. Know the default rollback rule, the self-invocation limit and the propagation modes, keep transactions short, and apply the annotation at the service layer.