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 runs, commits it if the method returns normally, and rolls it back if a runtime exception escapes. That gives you atomic, all-or-nothing business operations without writing begin, commit and rollback code yourself.
It works through a proxy that Spring wraps around your bean, so a few rules (public entry points, calls from outside the bean) decide whether the annotation takes effect at all. Those rules cause most real-world bugs, so this article covers them along with the attributes you will use every day.
Without it, a service method that touches several tables has to open a connection, disable auto-commit, run each statement, and then commit or roll back in a finally block. With it, you declare the intent and the framework does the plumbing:
This is called declarative transaction management. The business code stays free of transaction code, and the policy (isolation, timeout, rollback rules) sits in one annotation.
When a bean has @Transactional on it, Spring does not hand you the raw object. It injects a proxy, and the proxy delegates to a transaction interceptor. The interceptor asks the configured PlatformTransactionManager to begin a transaction, calls your method, and then commits or rolls back.
If withdraw throws, the deposit never happens and the debit is rolled back. In a Spring Boot application you do not need @EnableTransactionManagement: Boot auto-configures it and picks a JpaTransactionManager, DataSourceTransactionManager or similar based on what is on the classpath.
| Attribute | Purpose | Default |
|---|---|---|
propagation | How to behave when a transaction already exists | REQUIRED |
isolation | Visibility of concurrent changes | The database default |
readOnly | Hint that the method does not write | false |
timeout | Seconds before the transaction is marked for rollback | No timeout |
rollbackFor / noRollbackFor | Exceptions that force or prevent rollback | Unchecked exceptions and errors roll back |
REQUIRED joins an existing transaction or starts one. REQUIRES_NEW suspends the current transaction and runs in a separate one, which is useful for audit logging that must survive a rollback of the main work. SUPPORTS, MANDATORY, NOT_SUPPORTED, NEVER and NESTED cover the less common cases; NESTED uses savepoints and needs a transaction manager and driver that support them.
@Transactional(readOnly = true) signals that the method only reads. With JPA and Hibernate, Spring also stops the persistence context from flushing, which skips dirty checking, and some drivers and databases can route the work to a replica or optimise it. It is a hint, not an enforced guard.
By default Spring rolls back on RuntimeException and Error only. A checked exception commits the transaction unless you opt in:
Because the proxy sits in front of the bean, anything that bypasses the proxy bypasses the transaction.
this.save() from another method in the same class never goes through the proxy, so the @Transactional on save() is ignored. Move the method to another bean, or restructure so the transactional method is the entry point.new has no proxy.If an inner transactional method marks the shared transaction rollback-only and the outer method then returns normally, Spring throws UnexpectedRollbackException at commit time. That is the framework telling you a rollback was requested somewhere in the call chain.
Put it on service methods that represent a business operation, not on controllers or on every repository call. The service layer is where one use case spans several repositories and must be atomic. Annotating the class applies it to every public method; a method-level annotation overrides the class-level one.
Keep transactions short. Do not call remote HTTP services or wait on user input inside one, because the database connection stays checked out for the whole time. Transactions are bound to the current thread, so work you hand to another thread, including a virtual thread you start yourself, does not take part in the caller's transaction.
Not by default. Only RuntimeException and Error trigger a rollback. Add rollbackFor = Exception.class or a specific exception type to change that.
Yes, Spring understands jakarta.transaction.Transactional too. The Spring annotation offers more options, such as readOnly and timeout, so most Spring projects use org.springframework.transaction.annotation.Transactional.
The usual causes are a self-invoked method, a non-public method, an object created with new, or no PlatformTransactionManager bean. Check the call goes through the Spring-managed proxy.
Use the class level for a service where every public method is transactional, and override on individual methods that need different settings, such as readOnly = true.
@Transactional wraps a bean method in a transaction managed by a proxy: commit on normal return, rollback on unchecked exceptions. Tune it with propagation, isolation, readOnly, timeout and rollback rules. Apply it at the service layer on public methods called from outside the bean, keep transactions short, and remember that self-invocation, swallowed exceptions and checked exceptions are the common reasons a rollback does not happen.