Hamish Burke | 2025-06-16
Related to: #databases
Concurrency Control Mechanism
- Useful to enforce the Isolation part of ACID, for example when using Immediate Update Algorithm
Lock based protocols
- Means another Transaction can't access something another one has read/wrote from until they have committed
T1 | T2 | comments |
---|---|---|
read_item(Saving); | ||
read_item(Saving); | Fails | |
write_item(Savings + 100); | ||
commit | now another transaction can write |
Two-Phase Locking (2PL)
- Growing Phase
- Transaction acquires all the locks it needs
- Shrinking Phase
- Transaction releases locks
- Guarantee
- Schedule of transactions is serialisable
- Drawback
- Lead to deadlock, T1 waiting for T2, T2 waiting for T1
- Enforces Isolation
- Strict 2PL
T1 | T2 | Comments |
---|---|---|
lock(Saving) | T1 acquires lock | |
read_item(Saving) | ||
lock(Saving) | T2 waits (T1 holds lock) | |
write_item(Saving) | ||
unlock(Saving) | T1 releases lock (shrinking) | |
lock(Saving) | T2 acquires lock | |
read_item(Saving) | ||
unlock(Saving) |