Quick revision
The short answer
readOnly = true is a hint that Spring passes to the transaction manager and the resources it controls. With Hibernate, Spring sets the session's flush mode to manual and marks it read-only, so Hibernate keeps no dirty-checking snapshots and never flushes changes; the JDBC connection may also be set read-only, which some drivers and databases turn into a real refusal of writes while others ignore it. So it does not guarantee that writes are blocked: a native or explicit write may still succeed depending on the database. Spring Data's SimpleJpaRepository marks its read methods readOnly = true for these performance benefits.
What to remember
In this answer, we followed readOnly = true through the layers. Spring passes it on as a hint, Hibernate switches to manual flush and read-only mode so it keeps no snapshots and never flushes, and the JDBC connection is marked read-only, which drivers such as PostgreSQL's turn into a database refusal of writes while others ignore it. It is therefore not a guarantee against writes, especially for native queries or joined transactions, and Spring Data's SimpleJpaRepository uses it on its read methods for the performance benefits.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
readOnly = true is a hint that Spring passes to the transaction manager and the resources it controls. With Hibernate, Spring sets the session's flush mode to manual and marks it read-only, so Hibernate keeps no dirty-checking snapshots and never flushes changes; the JDBC connection may also be set read-only, which some drivers and databases turn into a real refusal of writes while others ignore it. So it does not guarantee that writes are blocked: a native or explicit write may still succeed depending on the database. Spring Data's SimpleJpaRepository marks its read methods readOnly = true for these performance benefits.
Why this question is asked
readOnly = true is one of the most widely used attributes, and one of the most misunderstood. Some developers treat it as a safety lock that stops writes; others think it does nothing. The truth is in between and depends on three layers, Spring, Hibernate and the JDBC driver, each of which reacts to the flag in its own way. Interviewers ask this to see whether we can follow a setting through those layers and know what we can and cannot rely on. In this answer, we will see what Spring does with the flag, what Hibernate does, what the driver and database do, test whether writes are really prevented, and look at how Spring Data uses it.
1What Spring Does With the Flag
Spring itself does not enforce anything. When a transaction starts, the TransactionInterceptor passes readOnly as part of the transaction definition to the PlatformTransactionManager, and the manager passes it on to the resources it manages. Spring also exposes it to our own code through TransactionSynchronizationManager.isCurrentTransactionReadOnly(), which is how routing data sources send read-only transactions to a replica.
So the real effects are in the layers below. Which ones apply depends on the transaction manager: JpaTransactionManager informs Hibernate and the connection, DataSourceTransactionManager informs only the connection.
- SpringPasses the hint on; exposes
isCurrentTransactionReadOnly() - Hibernate sessionFlush mode MANUAL, default read-only: no snapshots, no flush
- JDBC connection
Connection.setReadOnly(true) - Driver and databaseMay start a READ ONLY transaction and reject writes, or ignore it
4 layers, from the top down. Step through them one at a time.
Each layer decides for itself what to do with the hint.
2What Hibernate Does
With JPA and Hibernate, Spring's HibernateJpaDialect makes two changes to the session for a read-only transaction. It sets the flush mode to MANUAL, so Hibernate never flushes pending changes to the database automatically, not before queries and not at commit. And it marks the session as read-only by default, so entities loaded in it are read-only.
The read-only mode is the main performance benefit. Normally, for every entity it loads, Hibernate keeps a copy of its state, called a snapshot, so that at flush time it can compare each entity with its snapshot and find changes; this is dirty checking. In read-only mode, Hibernate keeps no snapshots and does no dirty checking, which saves memory and CPU when a query loads thousands of entities.
Notice that this also means a read-only transaction silently discards changes made to entities. That is usually what we want for a query method, but it is a confusing bug if a write method is accidentally marked read-only, for example through a class-level annotation.
3What the JDBC Driver and Database Do
Spring also calls Connection.setReadOnly(true) on the JDBC connection, and here behaviour depends on the driver. The PostgreSQL JDBC driver, by default, starts the transaction as READ ONLY, and PostgreSQL then rejects writes with an error. MySQL Connector/J also issues a read-only transaction setting on servers that support it, and InnoDB rejects writes too, and can skip some internal bookkeeping for read-only transactions. Other drivers treat the flag only as a hint and ignore it. The details are configurable and vary between versions, so check your driver.
With DataSourceTransactionManager, there is also an enforceReadOnly option that makes Spring send SET TRANSACTION READ ONLY explicitly, for drivers that do not do it themselves.
4So Does It Prevent Writes?
Not as a guarantee. Through JPA entities, changes are not flushed, so normal entity updates are lost rather than written, but an explicit query such as a @Modifying JPQL update or a native SQL statement is executed directly. Whether that statement succeeds depends on whether the driver started a read-only database transaction, which depends on the driver, its settings and the database. In addition, if a read-only method joins an existing read-write transaction with REQUIRED, the outer transaction's settings apply and the hint has no effect at all.
So readOnly = true should be read as 'this transaction is meant to only read, please optimise for that', not as a security control. If writes must be impossible, the reliable tool is a database user with only SELECT permissions, or a connection to a read-only replica.
| Kind of write | Typical result |
|---|---|
| Modify a managed entity | Not flushed: change silently lost |
repository.save(new entity) | May not be flushed; depends on id generation, check your version |
@Modifying JPQL or native UPDATE | Executed; succeeds unless the database transaction is READ ONLY |
| PostgreSQL with default pgjdbc | Rejected: cannot execute ... in a read-only transaction |
Driver that ignores setReadOnly | Succeeds |
| Method joined an outer read-write tx | Outer settings apply: succeeds |
5How Spring Data Uses It
Spring Data JPA's SimpleJpaRepository, the class behind every JpaRepository, is annotated @Transactional(readOnly = true) at class level, and its write methods, such as save(), saveAll() and delete(), override that with a plain @Transactional. So when we call findById() outside any service transaction, it runs in a read-only transaction and gets Hibernate's optimisations automatically.
The same pattern works well for our own services: annotate the class with @Transactional(readOnly = true) and annotate write methods with @Transactional. Queries get the read-only benefits by default, and a write method has to say explicitly that it writes.
6Follow-up questions
Does readOnly = true make queries faster?
It reduces Hibernate's memory and CPU work because there are no snapshots or dirty checks, which matters when loading many entities. The SQL itself is not faster, though some databases do less internal bookkeeping for read-only transactions.
Can readOnly route queries to a replica?
Yes, with a routing DataSource such as AbstractRoutingDataSource that checks TransactionSynchronizationManager.isCurrentTransactionReadOnly(). It must be combined with a LazyConnectionDataSourceProxy so the connection is chosen after the transaction settings are known.
Should every query method be read-only?
Generally yes, at the service level, as long as the method really only reads. The main risk is marking a write method read-only by accident through a class-level annotation, so write methods should always carry their own @Transactional.
7Conclusion
In this answer, we followed readOnly = true through the layers. Spring passes it on as a hint, Hibernate switches to manual flush and read-only mode so it keeps no snapshots and never flushes, and the JDBC connection is marked read-only, which drivers such as PostgreSQL's turn into a database refusal of writes while others ignore it. It is therefore not a guarantee against writes, especially for native queries or joined transactions, and Spring Data's SimpleJpaRepository uses it on its read methods for the performance benefits.
Open with
readOnly = true is a hint, not a lock. Spring passes it through the transaction manager to the resources. With JPA and Hibernate, Spring sets the session's flush mode to manual and makes it read-only by default, so Hibernate doesn't keep snapshots of loaded entities, doesn't do dirty checking and never flushes.
Then, if they want more
That's the main benefit: less memory and CPU for queries that load many entities. It also means that if I modify an entity in a read-only transaction, the change is silently lost, which is a nasty bug if a write method is accidentally read-only. Spring also calls setReadOnly on the JDBC connection.
What happens then depends on the driver: the PostgreSQL driver by default starts a read-only transaction, so PostgreSQL rejects writes with cannot execute in a read-only transaction; MySQL can do something similar; other drivers just ignore it. So does it prevent writes? Not reliably.
A native or @Modifying query may still succeed depending on the driver and database, and if the method joins an outer read-write transaction the flag has no effect. If writes must be impossible, I'd use a database user without write permissions or a read-only replica.
Spring Data's SimpleJpaRepository uses the pattern I like for services too: readOnly = true on the class, and plain @Transactional on the write methods like save and delete.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Data JPA reference, Hibernate ORM documentation, Spring Framework reference, Transaction management
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does @Transactional(readOnly = true) do with Spring, Hibernate and the JDBC driver?...": https://scrivio-interview-library.netlify.app/databases/transactions/1ccb9ade24/