In Spring Boot, @Transactional is how you make a service method atomic: Boot auto-configures the transaction manager, so you only add the annotation, and all database work in that method commits or rolls back as one unit. The significance in a Boot application is that the setup is nearly invisible, which makes it easy to misuse. You need to know what Boot configures, where the annotation takes effect and which properties affect it.
This article covers the Boot defaults, a short working example, rollback behaviour, testing and the Boot settings that interact with transactions.
When spring-boot-starter-data-jpa or spring-boot-starter-jdbc is on the classpath, Boot registers a transaction manager (JpaTransactionManager for JPA, DataSourceTransactionManager for plain JDBC) and enables annotation-driven transactions. You do not add @EnableTransactionManagement. Boot also defaults to proxying with CGLIB (spring.aop.proxy-target-class=true), so classes without an interface are proxied too.
With only this, any @Service method annotated @Transactional is transactional.
When name is "error", the exception leaves the method and the change to the user is not saved. With any other value, the transaction commits and the entity change is written through dirty checking. Note that no call to save is needed because the entity is managed.
An unchecked exception rolls back automatically. A checked one does not, so declare it:
| Property | Effect |
|---|---|
spring.jpa.open-in-view | Default true: keeps the JPA session open for the whole web request. Many teams set it to false and load data inside the service. |
spring.transaction.default-timeout | Default timeout in seconds for all transactions. |
spring.transaction.rollback-on-commit-failure | Roll back if the commit itself fails. |
spring.datasource.hikari.maximum-pool-size | Limits concurrent transactions, because each active one holds a connection. |
Boot also logs a warning on startup when open-in-view is left at its default. The setting does not extend the transaction; it only keeps the session available for lazy loading in the view layer, which hides queries and holds a connection longer.
@DataJpaTest and @SpringBootTest tests annotated with @Transactional roll back after each test by default, which keeps the database clean. That convenience has a catch: your test runs inside a transaction, so it will not notice a missing @Transactional in the service, and no commit ever happens. For integration tests that need real commits, avoid the test-level annotation and clean up explicitly, or use Testcontainers with a real database.
@Transactional on a controller. Keep controllers thin and the boundary in the service.@Async and expecting the caller's transaction. The async method runs on another thread with its own transaction context.@Scheduled and message listeners are normal entry points: annotate the method they call, not the framework callback.No. Boot enables it automatically when a transaction manager is on the classpath.
JpaTransactionManager with Spring Data JPA, DataSourceTransactionManager with plain JDBC. With several data sources, define the managers yourself and name the one to use in @Transactional("name").
Common causes are a checked exception without rollbackFor, an exception caught inside the method, or a self-invoked method that bypassed the proxy.
It is a widely recommended choice for production APIs, because it avoids hidden lazy queries and long connection use. You then fetch needed data inside the service.
Spring Boot auto-configures transaction management, so adding @Transactional to a public service method is enough to get commit and rollback. Declare rollback rules for checked exceptions, keep transactions short, review open-in-view, and remember that test-level transactions can hide missing annotations.