@Transactional(readOnly = true) tells Spring that a transaction will only read data. Spring passes that as a hint to the transaction infrastructure: Hibernate skips dirty checking and flushing, the JDBC connection can be marked read-only, and the database or a routing data source may optimise accordingly. It is not a security guard, so it does not guarantee that writes are rejected.
The flag helps most on query-heavy service methods, and Spring Data JPA already applies it for you on repository read methods.
When Spring opens a transaction for a method marked readOnly = true, the PlatformTransactionManager receives a TransactionDefinition whose isReadOnly() returns true. What happens next depends on the transaction manager and the persistence layer:
| Layer | Effect of the read-only hint |
|---|---|
JpaTransactionManager with Hibernate | The session flush mode is set to manual, so Hibernate does not flush at commit, and entities are loaded as read-only by default, which skips snapshot comparison |
DataSourceTransactionManager (plain JDBC, JdbcTemplate) | Calls Connection.setReadOnly(true) on the connection |
| JDBC driver and database | Depends on the driver. Some databases reject writes inside a read-only transaction, others only use it as an optimisation hint |
| Routing data sources | You can route read-only transactions to a replica (see below) |
The exact behaviour is documented per transaction manager and driver, so treat readOnly as an optimisation hint and not as an access-control mechanism.
Inside this method all repository calls share one transaction and one persistence context. Lazy associations can be loaded safely, and Hibernate does not spend time checking the loaded entities for changes when the method returns.
In a normal read-write transaction Hibernate keeps a snapshot of every managed entity it loads. At flush time it compares each entity with its snapshot (dirty checking) to decide which UPDATE statements to send. For a method that loads hundreds of entities and changes none of them, that is wasted memory and CPU.
With the read-only hint, Spring's Hibernate integration sets the session flush mode to manual and marks the session read-only by default. Hibernate then has no reason to compare entities at commit. The saving is real for large result sets and negligible for a single-row lookup.
SimpleJpaRepository, the default implementation behind JpaRepository, is annotated with @Transactional(readOnly = true) at class level. Methods that change data, such as save and delete, override it with a plain @Transactional. That means findById, findAll and derived query methods already run in read-only transactions.
Your own service layer is where you add the annotation. A common pattern is to put it on the class and override it on the methods that write:
Do not rely on a read-only transaction to reject writes. With Hibernate and manual flush mode, a change to a managed entity inside a read-only transaction is usually just never flushed, so it is silently lost. An explicit entityManager.flush() or a native statement can still reach the database depending on the driver and database.
If you want a hard failure, test it: write a small integration test that calls the method and then asserts that no data changed. Never use readOnly = true as a substitute for authorisation or database permissions.
Because the flag is part of the transaction definition, an AbstractRoutingDataSource can inspect it and choose a data source:
Wrap the routing data source in a LazyConnectionDataSourceProxy. Without it, Spring fetches the connection before it has set the read-only flag, and the lookup key is wrong. Remember replication lag: a read-only method that runs right after a write may not see that write on a replica.
@Transactional method on this bypasses the proxy and its settings are ignored.REQUIRED propagation, and the outer settings win. Use REQUIRES_NEW if the inner method needs its own read-only transaction.jakarta.transaction.Transactional only if you accept that it has no readOnly attribute. Spring's org.springframework.transaction.annotation.Transactional is the one that carries this setting.Usually a little. It avoids dirty checking and flushing in Hibernate and can let the database or a replica router do less work. Measure before claiming a specific gain for your workload.
You should not. In a Hibernate setup the write is typically not flushed, which hides a bug instead of failing loudly. Remove the flag from methods that change data.
No. Lazy loading needs an open persistence context, which any transaction provides. The read-only flag only adds the optimisations described above.
It is a reasonable default for query services. For mixed services, set readOnly = true on the class and override it with a plain @Transactional on the write methods.
@Transactional(readOnly = true) marks a transaction as read-only so Spring, Hibernate and the JDBC driver can skip work such as dirty checking and flushing, and so you can route reads to a replica. It is a hint, not enforcement. Spring Data repositories already use it for reads, so add it to your own query-only service methods and keep write methods on a plain @Transactional.