Concurrency Control
Concurrency Control
1
Lock-Based Protocols (Cont.)
■ Lock-compatibility matrix
2
Lock-Based Protocols (Cont.)
3
Pitfalls of Lock-Based Protocols
■ Consider the partial schedule
4
Pitfalls of Lock-Based Protocols (Cont.)
5
The Two-Phase Locking Protocol
6
The Two-Phase Locking Protocol (Cont.)
7
The Two-Phase Locking Protocol (Cont.)
8
Lock Conversions
– First Phase:
★ can acquire a lock-S on item
★ can acquire a lock-X on item
★ can convert a lock-S to a lock-X (upgrade)
– Second Phase:
★ can release a lock-S
★ can release a lock-X
★ can convert a lock-X to a lock-S (downgrade)
■ This protocol assures serializability. But still relies on the
programmer to insert the various locking instructions.
9
Automatic Acquisition of Locks
11
Implementation of Locking
12
Lock Table
■ Black rectangles indicate granted
locks, white ones indicate waiting
requests
■ Lock table also records the type of
lock granted or requested
■ New request is added to the end of
the queue of requests for the data
item, and granted if it is compatible
with all earlier locks
■ Unlock requests result in the
request being deleted, and later
requests are checked to see if they
can now be granted
■ If transaction aborts, all waiting or
granted requests of the transaction
are deleted
★ lock manager may keep a list of
locks held by each transaction, to
implement this efficiently
13
Graph-Based Protocols
14
Tree Protocol
15
Graph-Based Protocols (Cont.)
16
Timestamp-Based Protocols
17
Timestamp-Based Protocols (Cont.)
18
Timestamp-Based Protocols (Cont.)
19
Example Use of the Protocol
T1 T2 T3 T4 T5
read(X)
read(Y)
read(Y)
write(Y)
write(Z)
read(Z)
read(X)
abort
read(X)
write(Z)
abort
write(Y)
write(Z)
20
Correctness of Timestamp-Ordering Protocol
■ The timestamp-ordering protocol guarantees serializability since
all the arcs in the precedence graph are of the form:
transaction transaction
with smaller with larger
timestamp timestamp
21