@Transactional is Spring's annotation for declarative transaction management. It tells Spring to start a transaction before a method runs, commit it when the method returns normally and roll it back when a runtime exception escapes, so you do not write begin, commit and rollback code yourself.
Spring implements this with AOP proxies. That detail explains almost every surprise people hit with the annotation, such as self-invocation not starting a transaction, so it is worth understanding before relying on it.
When a bean has @Transactional methods, Spring wraps it in a proxy. A call from another bean goes through the proxy, which asks a PlatformTransactionManager to open (or join) a transaction, calls your method, and then commits or rolls back.
Both account updates succeed together or not at all, which is the atomicity guarantee of ACID.
In Spring Boot, the transaction infrastructure is auto-configured when a data source or JPA is present, so you only add the annotation. In a plain Spring application, add @EnableTransactionManagement to a configuration class and define a transaction manager bean such as JpaTransactionManager or DataSourceTransactionManager.
Use the Spring annotation org.springframework.transaction.annotation.Transactional. The Jakarta jakarta.transaction.Transactional annotation also works with Spring but supports fewer attributes.
| Attribute | Purpose | Default |
|---|---|---|
propagation | How to behave when a transaction already exists | REQUIRED |
isolation | Isolation level for the transaction | DEFAULT (database default) |
readOnly | Hint that no writes happen | false |
timeout | Seconds before the transaction times out | none |
rollbackFor / noRollbackFor | Exceptions that force or prevent rollback | runtime exceptions and errors roll back |
REQUIRED: join the current transaction or start a new one. This is the default.REQUIRES_NEW: suspend the current transaction and start an independent one. Useful for audit logging that must survive a rollback.SUPPORTS, NOT_SUPPORTED, NEVER, MANDATORY: various degrees of "use one if present" or "must not have one".NESTED: a savepoint inside the current transaction, supported with JDBC transaction managers.Isolation levels (READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE) trade consistency against concurrency. Set one explicitly only when you have a reason, because the default depends on the database.
readOnly = true lets Spring and the persistence provider skip some work (for example Hibernate dirty checking) and may route to a read replica, but it is a hint, not a security guard.
By default Spring rolls back on unchecked exceptions (RuntimeException and Error) and commits on checked exceptions. This surprises many developers.
If you catch an exception inside the transactional method and swallow it, Spring never sees it and the transaction commits. If you need to roll back without throwing, call TransactionAspectSupport.currentTransactionStatus().setRollbackOnly().
this.save() from another method in the same class bypasses the proxy, so the annotation on save() is ignored. Move the method to another bean or inject the bean into itself carefully.@Transactional on public methods called from outside the bean. Annotating private methods has no effect.LazyInitializationException. Fetch what you need inside the transaction.@Async method does not take part in it.When you need fine control, use TransactionTemplate:
The usual causes are a checked exception (which commits by default), an exception caught and swallowed inside the method, a self-invocation that bypasses the proxy, or a non-public method.
A class-level annotation applies to every public method, and a method-level one overrides it. Many teams put it on the service class and mark read-only methods with readOnly = true.
REQUIRED: an existing transaction is reused, and a new one is started only when none exists.
Blocking transactions work on virtual threads because the transaction is still bound to the running thread. Reactive stacks need reactive transaction managers and the same annotation on reactive return types.
@Transactional gives you declarative, proxy-based transaction handling: it opens a transaction around a method and commits or rolls back automatically. Learn the defaults (REQUIRED, rollback on unchecked exceptions only), set rollbackFor, readOnly and propagation deliberately, and watch for self-invocation and visibility, which are the most common reasons it appears not to work.