@Transactional is Spring's declarative way to run a method inside a database transaction. Spring starts the transaction before the method, commits it if the method returns normally, and rolls it back if a runtime exception escapes, so a group of changes either all happen or none do.
It works through a proxy around your bean, which is also the source of most surprises: self-invocation, private methods and checked exceptions all behave differently than people expect.
When Spring finds @Transactional on a class or method, it wraps the bean in a proxy. A call from outside the bean goes through the proxy, which asks the PlatformTransactionManager to begin a transaction, invokes your method, then commits or rolls back. With Spring Boot and Spring Data JPA, the manager is a JpaTransactionManager that Boot configures automatically.
If decrease throws a RuntimeException, the order is never saved and the stock change is undone. Use the Spring annotation org.springframework.transaction.annotation.Transactional; Jakarta's jakarta.transaction.Transactional also works, but it has fewer options.
By default Spring rolls back on RuntimeException and Error, and commits on checked exceptions. Change that with rollbackFor or noRollbackFor.
Catching an exception inside the method and swallowing it also prevents a rollback, because the proxy never sees the failure.
| Attribute | Purpose | Default |
|---|---|---|
propagation | How the method joins an existing transaction | REQUIRED |
isolation | Visibility between concurrent transactions | Database default |
readOnly | Hint that the method does not write | false |
timeout | Seconds before the transaction is marked for rollback | None |
rollbackFor | Extra exception types that trigger rollback | Runtime exceptions |
REQUIRED joins the current transaction or creates one. REQUIRES_NEW suspends the current transaction and starts an independent one, which is useful for audit logs that must be saved even if the main work fails. MANDATORY throws if no transaction is active, and NEVER throws if one is.
@Transactional(readOnly = true) is a good default for query methods. With Hibernate it skips dirty checking and flushes, and some databases and drivers can route the call to a read replica.
Put it on public methods of service classes, not on repositories or controllers. Class-level @Transactional applies to every public method, and a method-level annotation overrides it.
Two limits come from the proxy mechanism:
@Transactional settings are ignored.private, protected and package-private methods are not intercepted with the default proxy configuration.rollbackFor.@Async work inside the method. The new thread does not share the transaction.LazyInitializationException.No. With the default proxy-based approach, only calls to public methods that pass through the proxy are intercepted.
@Transactional uses Isolation.DEFAULT, meaning whatever the database uses. That is READ COMMITTED for PostgreSQL and REPEATABLE READ for MySQL InnoDB.
The exception was probably checked, was caught inside the method, or the method was called from within the same class and skipped the proxy.
No. Spring Boot enables transaction management automatically when a transaction manager is on the classpath.
@Transactional turns a service method into an atomic unit of work with commit and rollback handled by Spring. Place it on public service methods, remember that only runtime exceptions roll back by default, avoid self-invocation, and choose propagation and readOnly deliberately.