AP Exams (Advanced Placement) · Databases
Isolation Levels and Locking
Which accidents a kiosk session may still see, from READ UNCOMMITTED to SERIALIZABLE, and how 2PL implements the stronger ones.
Same two cashiers, same Tea stock and tea-sales count. Isolation levels are a menu: each setting bans one more accident. This lesson walks READ UNCOMMITTED through SERIALIZABLE on those two stories, then shows 2PL as one mechanism that can implement the stronger rows.
- AP Exams (Advanced Placement)
- Hard level
- 6 concepts
- 2 practice questions
1Isolation is a menu of accidents
Isolation levels are named settings that say which of the accidents from the last lesson a session is still allowed to see. They are not extra ACID letters. They are how strictly the engine keeps T2 from seeing T1's in-flight or newly committed work. Stronger levels block more accidents and usually hold locks longer or keep a longer snapshot.
The ANSI SQL menu, weakest to strongest: READ UNCOMMITTED, READ COMMITTED, REPEATABLE READ, SERIALIZABLE. Each row of the next concepts adds one ban. We will keep using Tea stock and the tea-sales count so the same two stories show what each level still permits.
Figure. Weakest at the top. Each step down bans one more accident. SERIALIZABLE bans all three.
| Level | Dirty | Non-repeatable | Phantom |
|---|---|---|---|
| READ UNCOMMITTED | yes | yes | yes |
| READ COMMITTED | no | yes | yes |
| REPEATABLE READ | no | no | yes |
| SERIALIZABLE | no | no | no |
Which isolation level prevents dirty and non-repeatable reads but may still allow phantoms?
- Read Uncommitted
- Read Committed
- Repeatable Read
- Serializable
Repeatable Read freezes the rows you already read. A new matching row (phantom) can still appear. Serializable is the one that also bans phantoms.
2READ UNCOMMITTED still allows dirty reads
READ UNCOMMITTED is the weakest setting: T2 may read a value T1 has written but not committed. At the kiosk, T1 is restocking Tea and writes 50. T2, under READ UNCOMMITTED, may see 50 and print a "we have plenty" sign. T1 then aborts; stock is 8. The sign lied. That is the dirty-read row of the menu, allowed on purpose at this level.
Almost no production cashier should sit here. The level exists so you can name the accident, and so engines that skip locking on reads have a standard name for that choice. The next level's whole job is to ban this one case.
Figure. READ UNCOMMITTED lets T2 see T1's unfinished 50. After T1 aborts, that 50 was never real.
The 50 that never committed
- T1WRITE stock = 50; not committed.
- T2 at RUREAD stock → 50.
- T1 abortstock = 8. T2's 50 was never a fact.
Under READ UNCOMMITTED, T2 reads Tea stock 50 written by uncommitted T1. This is
- Forbidden — READ UNCOMMITTED already bans dirty reads
- Allowed — that is what UNCOMMITTED means
- A phantom, because 50 is a new number
UNCOMMITTED means T2 may see T1's unfinished write. The next level, READ COMMITTED, is the one that bans it.
3READ COMMITTED bans dirty reads
READ COMMITTED says T2 only sees values whose writer has committed. The unfinished 50 is invisible; T2 waits or sees the old 8. Dirty reads are gone. Non-repeatable reads remain: T2 reads hostel A, T1 commits Asha's move to C, T2 reads again and sees C. Both values were committed at the moment they were read.
This is the default in several engines because "never show me a write that might vanish" is the minimum a cashier needs, and it still lets T1's committed move become visible on T2's next read. If T2 must see the same Asha row twice, we need the next level.
Figure. Two committed answers for the same Asha row. READ COMMITTED allows this; REPEATABLE READ will not.
Same Asha row, two committed answers
- T2 first readhostel = A (committed).
- T1 commitsUPDATE student SET hostel = 'C' WHERE student_id = 1; COMMIT.
- T2 second readhostel = C. Not dirty — both reads saw a committed value.
| Story | Allowed? |
|---|---|
| Dirty stock 50 | no |
| Asha A then C on re-read | yes — non-repeatable |
| New tea sale appears on re-count | yes — phantom |
T2 under READ COMMITTED reads Asha's hostel twice and sees A then C. T1 committed the move in between. This is
- A dirty read, so READ COMMITTED must have forbidden it
- A non-repeatable read, which READ COMMITTED still allows
- A phantom, because C is a new hostel
Same row, two committed values. That is non-repeatable, not dirty. RC bans only dirty reads.
4REPEATABLE READ freezes seen rows
REPEATABLE READ says a row T2 already read will not change under T2's feet. Asha stays in hostel A for the rest of T2, even if T1 commits a move to C — T2's second read still sees A (or T1 waits). Dirty and non-repeatable reads are gone.
Phantoms remain. T2 counts tea sales and gets 2. T1 commits sale 104 (Meera buys another tea). T2 counts again and gets 3. No old row changed; a new row appeared in the search. That new matching row is the phantom REPEATABLE READ still allows. SERIALIZABLE is the setting that also freezes the search.
Figure. Same WHERE, one extra tea row the second time. REPEATABLE READ still allows that phantom.
Count teas twice
- T2 count 1WHERE item_id = 11 → 2 rows (101, 103).
- T1 commitsINSERT sale 104, Meera, Tea, qty 1.
- T2 count 23 rows. The new 104 is a phantom.
| Accident | Allowed? |
|---|---|
| Dirty 50 | no |
| Asha A then C | no |
| Tea count 2 then 3 | yes — phantom |
Phantom tea row
Sale starts with two tea rows. T2 counts them (2). T1 inserts sale 104 for Tea and commits. T2 counts again under REPEATABLE READ. What are the two counts?
- tea rows at start2
- after insert 1043
- second COUNT under RR3 (phantom allowed)
Pro tip. The old rows did not change. A new row matched the same WHERE. That is a phantom, not a non-repeatable read.
Under REPEATABLE READ, T2's tea count goes from 2 to 3 because T1 inserted a row. This is
- Forbidden — REPEATABLE READ bans phantoms
- A phantom, which REPEATABLE READ still allows
- A dirty read of an uncommitted insert
T1 committed. The new row is a phantom. SERIALIZABLE is the level that also bans it.
5SERIALIZABLE bans the phantom too
SERIALIZABLE is the strongest ANSI level: the result must match some serial order of the transactions. Dirty, non-repeatable and phantom reads are all banned. T2's tea count stays 2 until T2 finishes, or T1's insert waits. Asha's hostel stays A. Uncommitted 50 is invisible.
It is not free. The engine holds range locks or keeps a snapshot that includes "there is no third tea sale". That is why cashiers doing a stock-take use it, and why a busy lunch rush might sit at READ COMMITTED and accept that a second count can grow.
Figure. Under SERIALIZABLE the tea search is frozen. Sale 104 waits, or T2 keeps seeing two rows.
| Story | Allowed? |
|---|---|
| Dirty stock 50 | no |
| Asha A then C | no |
| Tea count 2 then 3 | no |
The only ANSI level that also prevents phantom reads is
- Read Committed
- Repeatable Read
- Serializable
REPEATABLE READ still allows a new matching row. SERIALIZABLE does not.
6Two-phase locking
Locks are one way an engine implements those levels. Two-phase locking (2PL) has a growing phase — acquire locks, never release — then a shrinking phase — release locks, never acquire. That discipline produces conflict-serializable schedules. It does not, by itself, prevent deadlock: two cashiers can each hold Tea and wait for Samosa.
Strict 2PL holds exclusive locks until commit (or abort). That extra hold stops T2 reading a write T1 might still abort — it is how you get recoverability and a clean READ COMMITTED / stronger story, at the cost of T2 waiting until T1 finishes. Growing then shrinking is the shape; hold-until-commit is the stricter variant.
Figure. Two phases, one direction. Crossing back — unlocking then locking again — is not 2PL.
2PL on Tea stock
- GrowT1 locks stock, writes 50. No unlocks yet.
- Commit / shrinkT1 commits, then releases. Strict 2PL releases only now.
- T2May lock and read 8 or 50 only after that release — never the in-flight 50.
| Phase | May acquire? | May release? |
|---|---|---|
| Growing | yes | no |
| Shrinking | no | yes |
| Strict 2PL | until commit | exclusive locks at commit |
Lost update, and how strict 2PL stops it
Tea stock is 8. Two restock transactions each run stock := stock + 10. With no locking they interleave freely; under strict two-phase locking they cannot. What is the final stock in each case?
- no locks: both read 8, both compute 18, both write 18final stock 18, one +10 is lost
- strict 2PL: T1 takes an exclusive lock, writes 18, holds it until commitT2 blocks until T1 commits
- T2 then reads 18, writes 28, commitsfinal stock 28, no update lost
Pro tip. A read-modify-write race loses an update because both transactions read the same stale value; strict 2PL serialises them by holding the exclusive lock until commit.
During the shrinking phase of 2PL a transaction
- Can release locks but cannot acquire any new locks
- Acquires locks but cannot release any
- Aborts automatically
Growing = acquire only. Shrinking = release only. Strict 2PL delays those releases until commit.
Notes
- ANSI levels weakest to strongest: RU, RC, RR, SERIALIZABLE.
- RU allows dirty reads; RC bans them; RR also bans non-repeatable reads; SERIALIZABLE also bans phantoms.
- 2PL: grow (acquire only) then shrink (release only). Guarantees conflict serializability, not deadlock-freedom.
- Strict 2PL holds exclusive locks until commit, which prevents dirty reads of uncommitted writes.
Formulas
- RR = no dirty, no non-repeatable; phantoms remain.
- SERIALIZABLE ≃ some serial order.
- 2PL = growing then shrinking.
Exam traps & shortcuts
- Phantom = new row in the same search, not a changed old row.
- RC is "committed at the moment I read", not "the same forever".
Reference tables
| Level | Still allows |
|---|---|
| READ UNCOMMITTED | dirty, non-repeatable, phantom |
| READ COMMITTED | non-repeatable, phantom |
| REPEATABLE READ | phantom |
| SERIALIZABLE | none of those three |
Recap
Four names, three accidents, one locking shape.
- RU
- May read uncommitted 50.
- RC
- No dirty. Asha may still move A → C between reads.
- RR
- Seen rows freeze. Tea count may still grow (phantom).
- SZ
- Matches some serial order. Count stays 2.
- 2PL
- Grow then shrink. Strict = exclusive locks until commit.
Practise Isolation Levels and Locking
Reading is free and needs no account. Practice, mocks and progress live in the app.
- 2 exam-style questions on this topic, with explanations
- A 2-question practice set that ends the chapter
- Timed mocks scored with the real marking scheme
- Readiness tracked per topic, kept on your device