Skip to main content
Hint

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
TimeTransaction ATransaction BEffect
T1set isolation level read uncommitted
T2beginbegin
T3update students set name='bob' where id=1
T4select * from students where id=1Reads 'bob' (dirty read)
T5rollback
T6select * from students where id=1Still 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
TimeTransaction ATransaction BEffect
T1set isolation level read committedset isolation level read committed
T2beginbegin
T3select * from students where id=1Reads 'Alice'
T4update students set name='bob' where id=1
T5select * from students where id=1Still reads 'Alice' (uncommitted not visible)
T6commit
T7select * from students where id=1Reads 'bob' (non-repeatable read)
T8commit

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
Phantom 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.

TimeTransaction ATransaction BEffect
T1set isolation level repeatable readset isolation level repeatable read
T2beginbegin
T3select * from students where id=99Result is empty
T4insert into students (id, name) values (99, 'bob')
T5commit
T6select * from students where id=99Still empty (repeatable read)
T7update students set name='alice' where id=99Update succeeds
T8select * from students where id=99Data 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
Performance Impact

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 LevelDirty ReadNon-Repeatable ReadPhantom Read
READ_UNCOMMITTEDPossiblePossiblePossible
READ_COMMITTED-PossiblePossible
REPEATABLE_READ--Possible
SERIALIZABLE---