In Spring, @Transactional marks a class or method as a transactional unit: the framework opens a transaction before it runs, commits when it finishes normally and rolls back when an unchecked exception escapes. Its significance is that atomicity, isolation and rollback become configuration instead of hand-written try/catch/finally code, so related database changes either all happen or none do.
Spring implements this with AOP proxies and a PlatformTransactionManager. This article walks through the mechanism, the propagation and isolation settings, a realistic example and the rules that decide whether the annotation takes effect.
Spring supports two styles. Programmatic management uses TransactionTemplate and puts transaction code in your logic. Declarative management uses @Transactional and keeps the logic clean. Most applications use the declarative style and fall back to TransactionTemplate when they need a transaction around only part of a method.
The example also shows a good habit: the slow call happens before the transaction starts, so no database connection is held while waiting.
When the application context starts, Spring finds beans with @Transactional and wraps them in a proxy. A call from another bean goes through the proxy, which:
Spring Boot enables this automatically. In plain Spring configuration add @EnableTransactionManagement to a configuration class and define a transaction manager bean.
Propagation answers: what if a transaction already exists?
| Propagation | Meaning |
|---|---|
REQUIRED | Join the existing transaction, or create one (default) |
REQUIRES_NEW | Suspend the existing one and run in a new transaction |
SUPPORTS | Join if present, run without otherwise |
NOT_SUPPORTED | Suspend any transaction and run without |
MANDATORY | Fail if there is no transaction |
NEVER | Fail if there is a transaction |
NESTED | Run in a savepoint inside the existing transaction |
Isolation controls what concurrent transactions can see. The values map to the JDBC levels READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ and SERIALIZABLE; DEFAULT uses the database setting. Behaviour differs between databases. For example, PostgreSQL treats READ_UNCOMMITTED as READ_COMMITTED, so test with your real database.
OutOfStockException is a checked exception. Without rollbackFor, the transaction would commit even though the method failed. The timeout marks the transaction for rollback if it runs longer than ten seconds.
this.other() inside the same bean skips the proxy.new, not managed by Spring.@Async method, runs in its own transaction or none.Another subtle trap: Spring commits when a @Transactional method returns, which can be after your return statement runs. If you publish an event or message inside the method and the commit later fails, consumers saw data that never existed. Use @TransactionalEventListener(phase = AFTER_COMMIT) or an outbox table for side effects that must follow a successful commit.
Spring rolls back on RuntimeException and Error, and commits on checked exceptions unless rollbackFor says otherwise.
Put it on the implementing class or its methods. Annotations on interfaces work only with interface-based proxies and are not recommended.
REQUIRED shares one transaction with the caller, so a rollback affects both. REQUIRES_NEW uses a separate transaction that commits or rolls back independently.
Spring supports reactive transactions with ReactiveTransactionManager, where the transaction travels in the Reactor context, not a thread. The same annotation applies to methods returning Mono or Flux.
@Transactional makes a method atomic by wrapping it in a proxy-managed transaction. Choose propagation and isolation deliberately, declare rollback rules for checked exceptions, keep transactions short, and avoid the cases where the proxy is bypassed. Use TransactionTemplate when only part of a method needs a transaction.