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 a runtime exception escapes. You get atomic, all-or-nothing behaviour without writing begin, commit and rollback code yourself.
It works through proxies and a PlatformTransactionManager, such as JpaTransactionManager or DataSourceTransactionManager. Spring Boot configures the manager automatically when it finds a data source.
Without it, each repository call may commit on its own, so a failure halfway through a business operation leaves partial data. With it, the work between method entry and exit is one unit:
If debit throws, neither account changes. Use the annotation from org.springframework.transaction.annotation, which supports all attributes, not the jakarta.transaction.Transactional variant.
Spring wraps the bean in a proxy. A call from outside goes through the proxy, which opens the transaction, calls your method and then commits or rolls back. This has important consequences:
methodA() calls this.methodB() in the same class, the proxy is bypassed and methodB's @Transactional is ignored.final or static methods cannot be proxied this way.new are not transactional.Put the annotation on the service layer, where a business operation begins, rather than on repositories or controllers.
| Attribute | Purpose | Default |
|---|---|---|
propagation | How the method joins or creates transactions | REQUIRED |
isolation | Isolation level (READ_COMMITTED, SERIALIZABLE, ...) | Database default |
readOnly | Hint that no writes happen | false |
timeout | Seconds before the transaction is marked for rollback | None |
rollbackFor / noRollbackFor | Exception types that force or prevent rollback | Runtime and Error roll back |
readOnly = true lets Spring and the persistence provider skip dirty checking and, for some databases, route to a replica or optimise the connection. It does not stop writes on every database, so treat it as an optimisation and intent marker.
By default Spring rolls back on unchecked exceptions (RuntimeException and Error) and commits on checked exceptions. Change that with rollbackFor:
If you catch an exception inside the transactional method and swallow it, Spring never sees it and commits. Rethrow, or call TransactionAspectSupport.currentTransactionStatus().setRollbackOnly() when you must handle it locally.
Propagation controls what happens when a transactional method calls another one:
REQUIRED (default): join the current transaction, or start one if none exists.REQUIRES_NEW: suspend the current transaction and start an independent one.SUPPORTS: use a transaction if present, otherwise run without one.MANDATORY: require an existing transaction or throw.NOT_SUPPORTED: run without a transaction, suspending any current one.NEVER: throw if a transaction exists.NESTED: run inside a savepoint of the current transaction, where the transaction manager supports it (typically JDBC).rollbackFor.LazyInitializationException in JPA.@Async or new-thread code: the transaction is bound to the calling thread and does not carry over.On public service methods (or the service class) that represent one business operation. Avoid placing it on controllers and keep it narrow around the work that needs atomicity.
Not by default. Add rollbackFor = Exception.class or the specific exception type.
Both work in Spring, but Spring's own annotation exposes more options, such as readOnly, timeout and isolation. Jakarta's has rollbackOn and dontRollbackOn instead.
Usually because of self-invocation, a non-public method, a bean not managed by Spring, or a swallowed exception. Enable logging.level.org.springframework.transaction=DEBUG to see transaction boundaries.
@Transactional gives you declarative transactions: Spring opens, commits or rolls back for you around a method, using a proxy. Place it on service methods, know the default rollback rules, choose propagation and read-only settings deliberately, and watch for self-invocation and long-running transactions.