Concurrency Control
Concurrency Control
NEHA KATRE
Concurrency
Control
Schemes
NEHA KATRE
LOCK BASED PROTOCOL
• SHARED MODE LOCK: If transaction Ti has
obtained a shared mode lock on data item Q,
then Ti can read Q but cannot write Q.
Other transactions can also access the data
but in READ ONLY mode.
• EXCLUSIVE MODE LOCK: If transaction Ti has
obtained a exclusive mode lock on data item
Q, then Ti can both read and write Q.
Data item Q is not visible to other
transaction.
NEHA KATRE
• UPGRADING LOCKS: If a transaction changes
its mode of lock from shared mode to
exclusive mode it is called as upgrading of
locks.
• DOWNGRADING LOCKS: If a transaction
changes its mode of lock from exclusive
mode to shared mode it is called as
downgrading of locks.
NEHA KATRE
COMPATIBILITY FUNCTION
Ti now requests a lock of mode A
on the same data Q.
Tj If Ti can be granted the lock then
LOCK OF MODE we say mode A is compatible
B
with mode B.
DATA Q
Shared mode is compatible with
shared mode but it is not
compatible with exclusive mode.
NEHA KATRE
• It is not desirable for a transaction to unlock
the data immediately after its final access.
• Serializability may not be ensured
NEHA KATRE
Initial Value of A = 100 and B = 200
T1: Lock-X(B) T2: Lock-S(A)
Read(B) Read(A)
B:=B-50 Unlock(A)
Write(B) Lock-S(B)
Unlock (B) read(B)
Lock-X(A) Unlock (B)
Read(A) display (A+B)
A:=A+50
Write(A)
Unlock(A)
NEHA KATRE
NEHA KATRE
With delayed unlocking
NEHA KATRE
Granting Locks
The concurrency control manager can grant Ti
lock of mode M on data Q if
1.There is no other transaction holding a lock on
Q in a mode that conflicts with M
2.There is no other transaction that is waiting
for a lock on Q and that made its lock request
before Ti
NEHA KATRE
TWO PHASE LOCKING PROTOCOL
• Growing Phase: Transactions can acquire
locks but cannot issue unlocks.
• Shrinking Phase: Transactions can issue
unlock commands to release the data it had
locked. No locks can be issued.
NEHA KATRE
T0 T1
Lock_x(A)
i ng
row e Lock_X(B)
G as Lock_s(C)
Ph
A=A+B
B=B+C
Write(A)
Write(B)
as ing
Unlock (A)
Ph rink
Unlock (B)
e
Lock_x(A)
Sh
Unlock(C)
Read (A)
Read (B)
NEHA KATRE
Cascading rollback can occur in 2PL
NEHA KATRE
Strict Two-Phase Locking Protocol
• This protocol require
– 2PL
– All exclusive mode locks be realized only after
transaction commits
NEHA KATRE
DBMS: Hashing Chapter
NEHA KATRE
Implementation of Locking
NEHA KATRE
TIMESTAMP BASED PROTOCOLS
NEHA KATRE
• Determines the order in which transactions
are to be executed.
• To decide the order, a TIMESTAMP is
allocated to the transaction when it issues a
request for the data.
NEHA KATRE
There are two methods for implementing
timestamps
1. current time on system clock
NEHA KATRE
These techniques can be implemented
using the following validations
• W-Time Stamp: is the largest time-stamp of
any transaction that executed write(Q)
successfully.
NEHA KATRE
THE TIMESTAMP ORDERING PROTOCOL
NEHA KATRE
• 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.
NEHA KATRE
What will happen if we apply
timestamp ordering protocol?
NEHA KATRE
THOMAS WRITE RULE
• 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.
NEHA KATRE
Consider the following schedule. Assume TS(T1) =100, TS(T2) =200 and TS(T3)
=300.Apply timestamp ordering protocol to the following schedule justify the rule
applied at each step.
T1 T2 T3
R(A)
R(B)
W(C)
R(B)
R(C)
W(B)
W(A)
NEHA KATRE
VALIDATION BASED PROTOCOL
• Read phase:
• Write phase:
NEHA KATRE
• Each transaction Ti has 3 timestamps
1. Start(Ti) : the time when Ti started its execution
NEHA KATRE
Validation Test for Transaction Tj
• If for all Ti with TS (Ti) < TS (Tj) either one of
the following condition holds:
– finish(Ti) < start(Tj)
– start(Tj) < finish(Ti) < validation(Tj) and the set of
data items written by Ti does not intersect with
the set of data items read by Tj.
NEHA KATRE