Professional Documents
Culture Documents
38
Concurrency control schemes
The DBMS must provide a mechanism that
will ensure all possible schedules are:
serializable
recoverable, and preferably cascadeless
Concurrency control protocols ensure these
properties.
39
Lock-Based Concurrency Control
Lock - associated with some object
shared or exclusive
Locking protocol - set of rules to be followed
by each transaction to ensure good
properties.
40
Lock Compatibility Matrix
41
Transaction performing locking
T1
lock-X(A)
R(A)
W(A)
unlock(A)
lock-S(B)
R(B)
unlock(B)
42
Two-Phase Locking (2PL)
Two-Phase Locking Protocol
Each Xact must obtain a S (shared) lock on object
before reading, and an X (exclusive) lock on object
before writing.
A transaction can not request additional locks
once it releases any locks.
• growing phase
• shrinking phase
Strict Two-Phase Locking (Strict 2PL)
Strict Two-phase Locking Protocol:
Each Xact must obtain a S (shared) lock on object
before reading, and an X (exclusive) lock on object
before writing.
A transaction can not request additional locks
once it releases any locks.
All X (exclusive) locks acquired by a transaction
must be held until completion (commit/abort).
Not admissible under 2PL
T1 T2
R(A)
W(A)
R(A)
W(A)
R(B)
W(B)
Commit
R(B)
W(B)
Commit
45
Lock-based protocols
T1 T2
X(A) granted
X(B) granted
X(B) queued
X(A) queued
50
Deadlock Detection
T1 T2
T4 T3
Deadlock Prevention
54
Anomalies with Interleaved Execution
55
Reading Uncommitted Data
T1: Transfer T2: Interest
“Dirty Read” R(A)
W(A)
R(A)
Inconsistent result of A is W(A)
exposed to transaction T2
R(B)
W(B)
Commit
R(B)
W(B)
Commit 56
Unrepeatable Reads
57
Overwriting Uncommitted Data
T1 T2
W(A)
W(B)
W(B)
Commit
W(A)
Commit
58
Transaction support in SQL
59
What should we lock?
T1 T2
Coarse locking:
T1 S-lock on Sailors;
T2 X-lock on Sailors
Finer-grained locking:
T1 S-lock on all rows with rating=8;
T2 X-lock on Joe’s tuple.
60
The Phantom Problem
T1 T2
61
The Phantom Problem
T1 locks all existing rows with rating=8.
But a new row satisfying condition could be
inserted
Phantom problem: A transaction retrieves a
collection of tuples and sees different results,
even though it did not modify the tuples itself.
Conceptually: must lock all possible rows.
Can lock entire table.
Better, use index locking.
62
Data
Index
Index Locking
r=1
If there is an index on the rating field using
Alternative (2), T1 should lock the index page
containing the data entries with rating = 8.
If there are no records with rating = 8, T1 must
lock the index page where such a data entry would
be, if it existed!
If there is no suitable index, T1 must lock all
pages, and lock the file/table to prevent new
pages from being added, to ensure that no
new records with rating = 8 are added.
Predicate Locking
65
Specifying isolation level in SQL
LEVEL = SERIALIZABLE
REPEATABLE READ
Less isolation
READ COMMITTED
READ UNCOMMITED
67
READ COMMITTED
T reads only changes made by committed
transactions
No value read/written by T is changed by
another transaction until T completes.
Value read by T may be modified while T in
progress.
Phantoms possible.
68
READ UNCOMMITTED
69
Summary of Isolation Levels
REPEATABLE No No Maybe
READ
SERIALIZABLE No No No
70
Summary
Concurrency control and recovery are among the
most important functions provided by a DBMS.
Users need not worry about concurrency.
System guarantees nice properties: ACID
This is implemented using a locking protocol
71