SLIDE 6 6
Serialisability Theorem
Any schedule of two-phase locking transactions is conflict serialisable
2PL Prevents Lost Update
T1 Read(X) X = X – 5 Write(X) COMMIT T2 WAIT WAIT WAIT Read(X) X = X + 5 Write(X) COMMIT
write-lock(X) unlock(X) write-lock(X) unlock(X)
2PL Prevents Uncommitted Update
T1 Read(X) X = X – 5 Write(X) ROLLBACK T2 WAIT WAIT WAIT Read(X) X = X + 5 Write(X) COMMIT
write-lock(X) unlock(X) write-lock(X) unlock(X)
The value of X is restored during rollback, before the write-lock is released
2PL Prevents Inconsistent Analysis
T1 Read(X) X = X – 5 Write(X) Read(Y) Y = Y + 5 Write(Y) COMMIT T2 WAIT WAIT WAIT WAIT WAIT WAIT Read(X) Read(Y) Sum = X + Y ...
write-lock(X) write-lock(Y) read-lock(X) unlock(X) unlock(Y)
Concurrency in SQL
(and most other DBMSs) is handled automatically
DELETE etc will obtain write locks
read lock – or may read an old cached value
protocol depends on the engine
Locking
Locking
Locking
Concurrency in MySQL
- Sometimes you might want to lock a resource
specifically for updating: SELECT ID FROM Artist WHERE Name = ‘Muse’; ... Some processing INSERT INTO CD VALUES (NULL, 2, ‘The Resistance’, 9.99, ‘Rock’);
- In the short time between these queries, the ID for
muse may have been written to