[← Six silent failures](05-six-silent-failures.md) · [Index](../README.md) # 6. Isolation, read-only, and settings that are ignored Transcript: [`05-isolation.txt`](output/05-isolation.txt). ## The levels | Level | Prevents | Cost | |---|---|---| | `READ_UNCOMMITTED` | nothing | lowest | | `READ_COMMITTED` | dirty reads | low — the default on Postgres, SQL Server, Oracle | | `REPEATABLE_READ` | dirty + non-repeatable reads | medium — the default on MySQL | | `SERIALIZABLE` | all of the above + phantoms | highest | `ISOLATION_DEFAULT` means "whatever the connection already has", which is the database's default and not something Spring chooses. ## Settings apply only where the transaction starts The finding worth taking away: ``` @Transactional(isolation = SERIALIZABLE) [outer] isolation=SERIALIZABLE REQUIRED inner joining it isolation=SERIALIZABLE plain @Transactional [outer] isolation=default inner declaring READ_UNCOMMITTED isolation=default ``` The inner method declares `@Transactional(isolation = READ_UNCOMMITTED)` and gets the default. It joined an existing physical transaction, whose isolation was fixed when it began. The declaration is not rejected, nothing is logged, and it simply has no effect. The same is true of `readOnly` and `timeout` on a participating scope. All three are properties of a physical transaction, and a participating scope does not have one of its own. Confirmed in the transaction manager's own log: `ISOLATION_SERIALIZABLE` appears on `Creating new transaction` lines and never on `Participating in existing transaction` lines, because participation carries no settings. **Make it loud:** set `validateExistingTransaction=true` on the transaction manager and a mismatched declaration becomes an exception instead of a silent no-op. It is off by default. ## `readOnly` does less than its name suggests `readOnly = true` is a **hint**. It does not make the database reject writes. What it does: - Sets the JDBC connection read-only flag, which some drivers act on and others ignore. - Puts Hibernate's `FlushMode` to `MANUAL`, so the persistence context does not dirty-check or flush — which is where the real performance benefit comes from on read paths. - Lets a routing datasource send the transaction to a replica. A write inside a `readOnly` transaction may silently do nothing (never flushed) or may fail, depending on the driver. "Silently does nothing" is the more common and more dangerous outcome, and it is a genuine seventh entry for [chapter 5](05-six-silent-failures.md)'s list. ## `timeout` Seconds, enforced by Spring for the operations it controls and passed to the JDBC statement timeout where supported. Applies only to a scope that starts a transaction — same rule as above.