Two Phase Locking Protocol
Two Phase Locking Protocol
By
Mrs. G. Kavitha
Lock-Based Protocols
• A lock is a mechanism to control concurrent access to
a data item
• Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as
well as written. X-lock is requested using lock-X
instruction.
2. shared (S) mode. Data item can only be read. S-
lock is
requested using lock-S instruction.
• Lock requests are made to the concurrency-control
manager by the programmer. Transaction can proceed
only after request is granted.
2
Lock-Based Protocols
• Lock-compatibility matrix
(Cont.)
7
8
Example
9
The following way shows how unlocking and locking work
with 2-PL.
Transaction T1:
Growing phase: from step 1-3
Shrinking phase: from step 5-7
Lock point: at 3
Transaction T2:
Growing phase: from step 2-6
Shrinking phase: from step 8-9
Lock point: at 6
10
The Two-Phase Locking Protocol
(Cont.)
• There can be conflict serializable schedules that
cannot be obtained if two-phase locking is used.
• However, in the absence of extra information (e.g.,
ordering of access to data), two-phase locking is
needed for conflict serializability in the following
sense:
• Given a transaction Ti that does not follow two-phase
locking, we can find a transaction Tj that uses two-
phase locking, and a schedule for Ti and Tj that is not
conflict serializable.
11
Strict Two-phase locking (Strict-2PL)
->The first phase of Strict-2PL is similar to 2PL.
In the first phase, after acquiring all the locks,
the transaction continues to execute normally.
13
Lock Conversions
• Two-phase locking with 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. 14
Automatic Acquisition of Locks
• A transaction Ti issues the standard read/write
instruction, without explicit locking calls.
• The operation read(D) is processed as:
if Ti has a lock on D
then
read(D)
else begin
if necessary wait until no other
transaction has a lock-X on D
grant Ti a lock-S on D;
read(D)
end 15
Automatic Acquisition of Locks
(Cont.)
• write(D) is processed as:
if Ti has a lock-X on D
then
write(D)
else begin
if necessary wait until no other transaction has any
lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
• All locks are released after commit or abort
16
Timestamp-Based Protocols
• Each transaction is issued a timestamp when it enters the
system. If an old transaction Ti has time-stamp TS(Ti), a new
transaction Tj is assigned time-stamp TS(Tj) such that TS(Ti)
<TS(Tj).
• The protocol manages concurrent execution such that the time-
stamps determine the serializability order.
• In order to assure such behavior, the protocol maintains for
each data Q two timestamp values:
• W-timestamp(Q) is the largest time-stamp of any
transaction that executed write(Q) successfully.
• R-timestamp(Q) is the largest time-stamp of any
transaction that executed read(Q) successfully. 17
Timestamp-Based Protocols
(Cont.)
• The timestamp ordering protocol ensures that any conflicting
read and write operations are executed in timestamp order.
• Suppose a transaction Ti issues a read(Q)
1. If TS(Ti) W-timestamp(Q), then Ti needs to read a
value of Q that was already overwritten.
Hence, the read operation is rejected, and Ti is
rolled back.
2. If TS(Ti) W-timestamp(Q), then the read operation is
executed, and R-timestamp(Q) is set to max(R-
timestamp(Q), TS(Ti)).
18
Timestamp-Based Protocols
(Cont.)
• Suppose that transaction Ti issues write(Q).
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is
producing was needed previously, and the system assumed
that that value would never be produced.
Hence, the write operation is rejected, and Ti is rolled
back.
2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write
an obsolete value of Q.
Hence, this write operation is rejected, and Ti is rolled
back.
3. Otherwise, the write operation is executed, and W-
timestamp(Q) is set to TS(Ti). 19
Example Use of the Protocol
A partial schedule for several data items for transactions with
timestamps 1, 2, 3, 4, 5
20
Correctness of Timestamp-Ordering Protocol
21
Recoverability and Cascade
Freedom
• Problem with timestamp-ordering protocol:
• Suppose Ti aborts, but Tj has read a data item written by Ti
• Then Tj must abort; if Tj had been allowed to commit earlier,
the schedule is not recoverable.
• Further, any transaction that has read a data item written by
Tj must abort
• This can lead to cascading rollback --- that is, a chain of
rollbacks
• Solution 1:
• A transaction is structured such that its writes are all
performed at the end of its processing
• All writes of a transaction form an atomic action; no
transaction may execute while a transaction is being written
• A transaction that aborts is restarted with a new timestamp
• Solution 2: Limited form of locking: wait for data to be 22
committed before reading it
• Solution 3: Use commit dependencies to ensure recoverability
Thomas’ Write Rule
• Modified version of the timestamp-ordering protocol in which
obsolete write operations may be ignored under certain
circumstances.
• When Ti attempts to write data item Q, if TS(Ti) < W-
timestamp(Q), then Ti is attempting to write an obsolete value
of {Q}.
• Rather than rolling back Ti as the timestamp ordering
protocol would have done, this {write} operation can be
ignored.
• Otherwise this protocol is the same as the timestamp ordering
protocol.
• Thomas' Write Rule allows greater potential concurrency.
23
• Allows some view-serializable schedules that are not
conflict-serializable.
Possible Questions
Part A:
1. Why do we need locks?
2. What is a lock?
3. What are the two types of lock?
4. Differentiate between shared lock and exclusive lock.
5.What type of lock is needed for insert and delete
operations?
Part B:
1. State and explain the lock based concurrency control with
suitable example.
24
Possible Questions:
Part A:
1. What is two phase locking protocol?
2. What are the limitations of two phase locking protocol?
3. List out the two types of two phase locking protocol?
4. What is Strict two phase locking protocol?
5. What is rigorous two phase locking protocol?
6. List out the advantages of Strict two phase locking protocol?
7. What is timestamp based protocol?
8. What is timestamp?
Part B:
1. Illustrate two phase locking protocol with an example.
2. Explain in detail about timestamp based protocol with
relevant example. 25