Skip to main content
Hint

This article is generated by AI translation.

Isolation Levels

Use the table below as the base data for the isolation level examples:

mysql> select * from students;
+----+-------+
| id | name |
+----+-------+
| 1 | Alice |
+----+-------+
1 row in set (0.00 sec)

DEFAULT

Uses the database default isolation level; the actual level depends on the underlying driver/database.

  • Constant: Isolation.DEFAULT
info

Effectively means “not set”; the driver decides the concrete behavior.

Dirty read (Read Uncommitted)

Read Uncommitted is the lowest isolation level. A transaction can read data modified but not yet committed by another transaction. If that transaction rolls back, the read data is dirty.

  • Constant: Isolation.READ_UNCOMMITTED
| Time  | Transaction A                                     | Transaction B                        | Effect                  |
|-------|---------------------------------------------------|--------------------------------------|-------------------------|
| 00:01 | set transaction isolation level read uncommitted; | | |
| 00:02 | begin; | begin; | |
| 00:03 | update students set name = 'bob' where id = 1; | | |
| 00:04 | | select * from students where id = 1; | see the change at 00:03 |
| 00:05 | update students set name = 'bob' where id = 2; | | |
| 00:06 | rollback; | | |
| 00:07 | | select * from students where id = 1; | see the change at 00:01 |
| 00:08 | | commit; | |

Non-repeatable read (Read Committed)

Read Committed can lead to non-repeatable reads: two identical queries in the same transaction can return different results.

  • Constant: Isolation.READ_COMMITTED
| Time  | Transaction A                                   | Transaction B                                   | Effect                  |
|-------|-------------------------------------------------|-------------------------------------------------|-------------------------|
| 00:01 | set transaction isolation level read committed; | set transaction isolation level read committed; | |
| 00:02 | begin; | begin; | |
| 00:03 | | select * from students where id = 1; | original data |
| 00:04 | update students set name = 'bob' where id = 1; | | |
| 00:05 | | select * from students where id = 1; | original data |
| 00:06 | commit; | | |
| 00:07 | | select * from students where id = 1; | see the change at 00:06 |
| 00:08 | | commit; | |

Repeatable read (Repeatable Read)

With Repeatable Read, all rows referenced by the transaction are locked for its duration.

  • Constant: Isolation.REPEATABLE_READ
info

In Repeatable Read, a transaction may still encounter phantom reads.

Phantom read: the first query finds no record; an attempt to update that non-existent record succeeds; querying again shows the record has “appeared.”

TimeTransaction ATransaction BEffect
00:01set transaction isolation level repeatable read;set transaction isolation level repeatable read;
00:02begin;begin;
00:03select * from students where id = 99;result is empty
00:04insert into students (id, name) values (99, 'bob');select * from students where id = 99;see the change at 00:03
00:05commit;
00:06update students set name = 'alice' where id = 99;update is accepted
00:07select * from students where id = 99;result is empty (Phantom Read)
00:08rollback;commit;

Serialized execution (Serializable)

Provides strict isolation by serializing transaction execution. Transactions run one after another (no concurrency), preventing dirty reads, non-repeatable reads, and phantom reads.

  • Constant: Isolation.SERIALIZABLE
info

Serializable effectively places an exclusive lock on the entire database for the duration of the transaction; other transactions cannot start until it commits.

Because this can severely impact throughput, Serializable is rarely chosen unless absolutely required.