Database Management System 22: Locking Protocols
Database Management System 22: Locking Protocols
Chittaranjan Pradhan
System 22
Lock Conversions
Timestamp-Based
Protocols
Locking Protocols
Timestamps
Timestamp Ordering
Protocols
Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule
Chittaranjan Pradhan
School of Computer Engineering,
KIIT University
22.1
Locking Protocols
Two-Phase Locking Protocol
Chittaranjan Pradhan
Two-Phase Locking
Two-Phase Locking Protocol Protocol
Lock Conversions
Two-phase locking protocol is a protocol which ensures Timestamp-Based
serializability Protocols
Timestamps
Timestamp Ordering
Protocols
This protocol requires that each transaction issues lock and Problems with Timestamp
Ordering Protocols
unlock requests in two phases. The two phases are: Thomas’ Write Rule
22.2
Locking Protocols
Two-Phase Locking Protocol...
Chittaranjan Pradhan
Two-Phase Locking
Protocol
Lock Conversions
Timestamp-Based
Protocols
Timestamps
Timestamp Ordering
Protocols
Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule
T4 T5 T6
Lock-X(A);
Read(A);
Lock-S(B);
Read(B);
Write(A);
Schedule7 Unlock(A);
Lock-X(A);
Read(A);
Write(A);
Unlock(A);
Lock-S(A);
Read(A);
22.4
Locking Protocols
Two-Phase Locking Protocol...
Chittaranjan Pradhan
Two-Phase Locking
Protocol
Lock Conversions
must hold all its exclusive locks till it commits or aborts Problems with Timestamp
Ordering Protocols
Thomas’ Write Rule
22.5
Locking Protocols
Lock Conversions
Chittaranjan Pradhan
T7
Read(a1);
Read(a2); Two-Phase Locking
Protocol
Read(a3);
Lock Conversions
...
Read(an); Timestamp-Based
Protocols
Write(a1); Timestamps
Timestamp Ordering
Protocols
T8
Problems with Timestamp
Read(a1); Ordering Protocols
Read(a3); Thomas’ Write Rule
Display(a1+a3);
Lock Conversions
Lock Upgrade: This is the process in which a shared lock is
upgraded to an exclusive lock
Thus, the two-phase locking protocol with lock conversions: Two-Phase Locking
Protocol
Timestamp-Based
• Can acquire a lock-S on item Protocols
Timestamps
• Can acquire a lock-X on item Timestamp Ordering
• Can convert a lock-S to a lock-X (upgrade) Protocols
Problems with Timestamp
Ordering Protocols
• Second Phase: Thomas’ Write Rule
If the exclusive locks are held until the end of the transaction,
then the schedules became cascadeless
22.7
Locking Protocols
Lock Conversions...
Chittaranjan Pradhan
T7 T8 Two-Phase Locking
Lock-S(a1); Protocol
Lock Conversions
Read(a1); Timestamp-Based
Lock-S(a1); Protocols
Timestamps
Read(a1); Timestamp Ordering
Lock-S(a2); Protocols
Problems with Timestamp
Read(a2); Ordering Protocols
Thomas’ Write Rule
Lock-S(a3);
Read(a3);
Schedule8 Lock-S(a3);
Read(a3);
Display(a1+a3);
Unlock(a1);
Unlock(a3);
...
Lock-S(an);
Read(an);
Upgrade(a1);
Write(a1);
22.8
Locking Protocols
Timestamp-Based Protocols
Chittaranjan Pradhan
Timestamp-Based Protocols
The timestamp method for concurrency control doesn’t need Two-Phase Locking
Protocol
any locks and therefore this method is free from deadlock Lock Conversions
situation Timestamp-Based
Protocols
Timestamps
Timestamp Ordering
Locking methods generally prevent conflicts by making Protocols
Problems with Timestamp
transaction to wait; whereas timestamp methods do not make Ordering Protocols
Thomas’ Write Rule
the transactions to wait. Rather, transactions involved in a
conflicting situation are simply rolled back and restarted
Two-Phase Locking
Timestamps Protocol
Lock Conversions
Timestamp TS(Ti ) is assigned by the database system before Timestamp-Based
the transaction Ti starts its execution Protocols
Timestamps
Timestamp Ordering
Protocols
The timestamps of the transactions determine the serializability Problems with Timestamp
Ordering Protocols
order. Thus, if TS(Ti ) < TS(Tj ), then the system must ensure Thomas’ Write Rule
22.10
Locking Protocols
Timestamp Ordering Protocols
Chittaranjan Pradhan
• If TS(Ti ) < W-TS(Q), then Ti needs to read a value of Q that Timestamp Ordering
Protocols
was already overwritten. Hence, the read operation is Problems with Timestamp
Ordering Protocols
rejected and Ti is rolled back Thomas’ Write Rule
Two-Phase Locking
Protocol
Lock Conversions
If a transaction Ti is rolled back by the concurrency-control Timestamp-Based
scheme, the system assigns it a new timestamp and restarts it Protocols
Timestamps
Timestamp Ordering
T1 T2 T1 T2 T1 T2 Protocols
Read(A); Read(A); Read(A); Problems with Timestamp
Ordering Protocols
A:=A-100; A:=A-100; A:=A-100;
Thomas’ Write Rule
Write(A); Read(A); Write(A);
Read(A); Temp:=0.2*A; Read(A);
Temp:=0.2*A; A:=A-Temp; Temp:=0.2*A;
A:=A-Temp; Write(A); A:=A-Temp;
Write(A); Read(B); Read(B);
Read(B); Write(A); B:=B+100;
B:=B+100; Read(B); Write(A);
Write(B); B:=B+100; Read(B);
Read(B); Write(B); B:=B+ Temp;
B:=B+ Temp; B:=B+ Temp; Write(B);
Write(B); Write(B); Write(B);
Schedule3 Schedule4 Schedule5
22.12
Locking Protocols
Timestamp Ordering Protocols...
Chittaranjan Pradhan
Two-Phase Locking
From the above examples, we can say that the timestamp Protocol
Lock Conversions
The protocol ensures freedom from deadlock, since no Thomas’ Write Rule
22.13
Locking Protocols
Problems with Timestamp Ordering Protocols
Chittaranjan Pradhan
Two-Phase Locking
Problems with Timestamp Ordering Protocols Protocol
Lock Conversions
22.14
Locking Protocols
Thomas’ Write Rule
Chittaranjan Pradhan
Two-Phase Locking
Thomas’ Write Rule Protocol
Lock Conversions
T9 T10
Read(Q);
Write(Q);
Schedule9
Write(Q);
Read(M);
Write(M);
22.15
Locking Protocols
Thomas’ Write Rule...
Chittaranjan Pradhan
Two-Phase Locking
Protocol
Lock Conversions
Thomas’ Write Rule... Timestamp-Based
Protocols
• Ti issues Read(Q): Timestamps
Timestamp Ordering
• Remains same Protocols
Problems with Timestamp
Ordering Protocols
• Ti issues write(Q): Thomas’ Write Rule
22.16