@Transactional(readOnly = true) marks a transaction as read-only. It is a hint, not a hard guarantee: Spring passes it to the transaction infrastructure so that the JDBC driver, the database and the persistence provider can optimise. With Spring Data JPA and Hibernate, it also switches the session to manual flush mode, which skips dirty checking and makes read paths cheaper.
Use it on service methods that only query data. It documents your intent and often improves performance, but you should not rely on it to prevent writes.
The effect depends on the transaction manager and the stack below it.
DataSourceTransactionManager calls Connection.setReadOnly(true) and restores the previous value afterwards. Some drivers send a read-only hint to the server. PostgreSQL starts the transaction as READ ONLY, so write statements fail with an error. Other databases may ignore the hint.MANUAL. Hibernate no longer flushes changes at commit and does not run dirty checking on loaded entities, which saves CPU and memory on large result sets. Hibernate can also skip keeping a loaded-state snapshot for read-only sessions.AbstractRoutingDataSource plus a LazyConnectionDataSourceProxy, the read-only flag can send queries to a replica.Because behaviour differs, test it against your own database. Do not assume a write inside a read-only transaction will fail.
The class-level annotation makes every method read-only by default. The method-level @Transactional on rename overrides it with the default read-write settings. This is the same pattern Spring Data uses in SimpleJpaRepository, which is read-only at class level, and its write methods override it.
readOnly is applied only when a new transaction is started. If a read-only method is called from an existing read-write transaction with the default REQUIRED propagation, it joins that transaction and the flag has no effect. Spring can warn about this if you enable validation:
With validation on, joining a read-write transaction from a read-only declaration throws an exception. The opposite case, a read-write method joining a read-only transaction, is a real bug: writes might not be flushed.
| Use it | Avoid it |
|---|---|
| Query methods and reports | Methods that insert, update or delete |
| Class-level default for a read service | Methods that need pending changes flushed |
| Read paths served from replicas | Code that calls a write inside the same transaction |
A read-only transaction still gives you a consistent snapshot according to the isolation level, so several queries in one method see consistent data.
Often, mainly with Hibernate because it avoids dirty checking and flushing. The gain depends on the number of loaded entities and the database. Measure before assuming.
It depends on the database. PostgreSQL rejects writes, while some databases accept them. With JPA the writes are simply not flushed. Never design code that depends on either outcome.
Not required, but it is good practice. It signals intent, enables optimisations and supports replica routing.
Either. Place it on the service so that a whole use case shares one read-only transaction, rather than one transaction per repository call.
@Transactional(readOnly = true) is a performance and intent hint for read-only work. It sets the JDBC read-only flag, makes Hibernate use manual flush mode and can enable replica routing. It applies only when the transaction starts, does not guarantee that writes fail, and should be used as a class-level default with write methods overridden.