Concurrency Control
Concurrency Control
T1 T2
X
X √ X
T1 W T1
A R
X R
B T2
W
T2 Database R
T3
T1 T2 T1 (Start Time=2)
X
A X A
√ TS = 3
A
T2 (Start Time=4)
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 concurrency-control
manager. Transaction can proceed only after
request is granted.
•
Lock-Based
Lock-compatibility matrix
Protocols (Cont.)
1: Requests
Concurrency Control Component T1
4: Granted/ Denied
2: Check 3: T/F
T1 T2 T1 T2
Lock-S (A)
Lock-S (A)
Read (A)
Read (A)
Lock-X(A)
Lock-S(A) Read (P)
• Simple Locking.
• 2 Phase Locking.
– Basic 2 Phase Locking (B2PL)
– Conservative 2 Phase Locking (C2PL)
– Strict 2 Phase Locking(S2PL)
– Rigorous 2 Phase Locking (R2PL)
Simple Locking
• Lock The Data Item before access.
• Release the Lock as soon as action is Over.
• Example:
1. Lock –X (A)
2. Read (A)
3. A=A+10
4. Write (A)
5. Unlock (A)
• When the Transaction executes serially, no issue.
• But when executed in interleaved manner may leads to inconsistent.
R(B)
R(B)
B=B+100
W(B) Unlock(B)
W(B)
Display (A +B)
Unlock (B)
T1 T2
Lock –X (A)
R(A)
A=A-100;
W(A)
Unlock(A) Lock-S (A)
R(A)
Unlock (A)
Lock-X(B)
R(B)
B=B+100
W(B)
Unlock (B) Lock-S(B)
R(B)
Unlock(B)
Display (A +B)
Pitfalls of Lock-Based Protocols (Cont.)
Lock Point
Unlock
R/W Lock
R/W
Locks Acquired Locks Released
Unlock
Lock
T1 B
A T2
• Cascading Rollback:
T1 T2 T3
Lock-X (A)
R(A)
Rollback
W(A)
U(A) Rollback
. Lock-X(A)
Rollback
. R(A)
Failure W(A)
U(A)
Lock-X(A)
R(A)
. . .
. . .
. . .
Conservative 2 Phase Locking Protocol
• Lock the Data items before Transaction Starts.
• Need early predictions of how many data
items may be required for transaction.
– T1 : [A B C D]
– Request Lock on { A B C D}
– If lock is granted operation starts.
– Otherwise it will wait further the request to be
granted.
Conservative 2 Phase Locking Protocol
Unlock
No Growing Phase
R/W
Locks Released
Unlock
Shrinking Phase
End Transaction
• Deadlock Never Occur.
• Early Prediction is difficult for practical
implementation.
• Cascading Rollback may present.
Strict Phase Locking Protocol
• Transaction does not release any of its lock Exclusive
Lock until it is committed/Aborted.
Lock Point
Unlock-S
Lock-X
R/W
Locks Acquired Locks Released
Lock-S End Transaction
Committed
Growing Phase Shrinking Phase
Unlock-X
• S2PL is Most Popular.
• Does not have cascading roll back.
• Deadlock May Present.
• Generates Strict Schedule.(Both Recoverable
and Cascade less.
• Easy Recovery.
Recoverable Schedule
• For each pair of transactions (Ti, Ti), if Tj reads the
item that was previously written by Ti, then commit
operation of Ti should appear before commit
operation Tj.
T1 T2
R1(A)
A= A+150
W(A)
R2(A)
A= A-50;
W2(A)
Commit;
Commit;
Strict Schedule
• If a value written by a transaction cannot be
read or over written by another transaction
until the transactions is either aborted or
committed.
• Every Strict Schedule is Recoverable and
Cascade less. T1 T2
R1 (A)
W1(A)
Commit;
W2(A)
R(A)
Rigorous Phase Locking Protocol
• Transaction does not release any of its Lock (Shared
and Exclusive) until it is committed or Aborted.
Lock Point End Transaction
Committed
R/W Lock-X
Unlock-S
Locks Acquired
Unlock-X
Lock-S
Start Transaction
No Shrinking Phase
Growing Phase
• Does not have cascading roll back.
• Deadlock May Present.
C2PL
B2PL
Lock Point
Unlock-S
Lock-X
R/W
Locks Acquired Locks Released
Lock-S End Transaction
Committed
Growing Shrinking Phase
S2PL Unlock-X
Phase
R2PL
Examples
T1
Lock-S(A) B2PL
R(A) C2PL
Lock-X(B) S2PL
R(A)
R2PL
R(B)
B=A+B
Unlock(A)
W(B)
Unlock(B)
T1
Lock-S(A)
R(A)
B2PL
Lock-X(B)
C2PL
Unlock(A) S2PL
R(B) R2PL
W(B)
Commit
Unlock(B)
T1
B2PL
Lock-S(A)
C2PL
R(A)
S2PL
Unlock(A)
R2PL
Lock-X(B)
R(B)
W(B)
Unlock(B)
Commit
Write Schedules Using B2PL
• Transaction 1: T1
Lock –S (A)
– Read (A)
R(A)
– Read (B) Lock –X (B)
– If A=0, B=B+1; R(B)
If A=0; B=B+1;
– Write (B); Unlock (A)
W(B)
Unlock(B)
Write Schedules Using C2PL
• Transaction 1: T1
R(B)
– If A=0, B=B+1;
If A=0; B=B+1;
– Write (B);
Unlock (A)
W(B)
Unlock(B)
Write Schedules Using S2PL
• Transaction 1: T1
Lock –S (A),
– Read (A)
R(A)
– Read (B) Lock –X(B)
– If A=0, B=B+1; R(B)
If A=0; B=B+1;
– Write (B); Unlock (A)
W(B)
Commit
Unlock(B)
Write Schedules Using R2PL
• Transaction 1: T1
Lock –S (A),
– Read (A)
R(A)
– Read (B) Lock –X(B)
– If A=0, B=B+1; R(B)
If A=0; B=B+1;
– Write (B); W(B)
Commit
Unlock(B)
Unlock (A)
Solve This
• Transaction 2:
– Read (B)
– Read (A)
– If B=0, A =A-B
– Write(A)