This article is generated by AI translation.
Isolation Levels
The examples for each isolation level use the following table:
mysql> select * from students;
+----+-------+
| id | name |
+----+-------+
| 1 | Alice |
+----+-------+
DEFAULT
Default transaction isolation level; the actual isolation level used is determined by the database driver.
- Constant:
Isolation.DEFAULT
Dirty Read (Read Uncommitted)
The lowest isolation level. Transaction A can read data updated by Transaction B that has not yet been committed. If Transaction B rolls back, Transaction A has read dirty data.
- Constant:
Isolation.READ_UNCOMMITTED
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | set isolation level read uncommitted | ||
| T2 | begin | begin | |
| T3 | update students set name='bob' where id=1 | ||
| T4 | select * from students where id=1 | Reads 'bob' (dirty read) | |
| T5 | rollback | ||
| T6 | select * from students where id=1 | Still reads 'Alice' |
Non-Repeatable Read (Read Committed)
Transaction A can only read data that other transactions have already committed. However, within the same transaction, two identical queries may return different results (because another transaction committed changes in between).
- Constant:
Isolation.READ_COMMITTED
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | set isolation level read committed | set isolation level read committed | |
| T2 | begin | begin | |
| T3 | select * from students where id=1 | Reads 'Alice' | |
| T4 | update students set name='bob' where id=1 | ||
| T5 | select * from students where id=1 | Still reads 'Alice' (uncommitted not visible) | |
| T6 | commit | ||
| T7 | select * from students where id=1 | Reads 'bob' (non-repeatable read) | |
| T8 | commit |
Repeatable Read (Repeatable Read)
During transaction execution, the same query always returns the same results, even if other transactions have committed changes.
- Constant:
Isolation.REPEATABLE_READ
Under Repeatable Read, a transaction may encounter a Phantom Read: the first query for a record finds it does not exist, but an update on that record succeeds, and querying again shows it has appeared.
| Time | Transaction A | Transaction B | Effect |
|---|---|---|---|
| T1 | set isolation level repeatable read | set isolation level repeatable read | |
| T2 | begin | begin | |
| T3 | select * from students where id=99 | Result is empty | |
| T4 | insert into students (id, name) values (99, 'bob') | ||
| T5 | commit | ||
| T6 | select * from students where id=99 | Still empty (repeatable read) | |
| T7 | update students set name='alice' where id=99 | Update succeeds | |
| T8 | select * from students where id=99 | Data appears (phantom read) |
Serializable (Serializable)
The highest isolation level. Transactions are serialized — no concurrency. Dirty reads, non-repeatable reads, and phantom reads are all prevented.
- Constant:
Isolation.SERIALIZABLE
Serializable effectively places an exclusive lock on the entire database when a transaction begins. Other transactions cannot start until the current one commits, greatly reducing efficiency. This level is generally not used unless absolutely necessary.
Comparison of Isolation Levels
| Isolation Level | Dirty Read | Non-Repeatable Read | Phantom Read |
|---|---|---|---|
| READ_UNCOMMITTED | Possible | Possible | Possible |
| READ_COMMITTED | - | Possible | Possible |
| REPEATABLE_READ | - | - | Possible |
| SERIALIZABLE | - | - | - |