concurrency new
concurrency new
O verview
●
Introduction
●
Locking Techniques
Introduction
●
Need for Concurrency Control
● When operations of different transactions are executed concurrently by
interleaving of operations, several problems are associated:
●
Purpose of Concurrency Control
– To enforce Isolation (through mutual exclusion)
a m on g conflicting transactions.
●
Lock and Unlock are Atomic operations.
S hared / Exclus iv e Lock
●
Two locks modes (a) shared (read) and (b) exclusive (write).
●
Shared mode: Shared lock (X).
•More than one transaction can apply share lock on X for reading its
value but no write lock can be applied on X by any other transaction.
●
Exclusive mode: Write lock (X).
•Only one write lock on X can exist at any time and no shared lock can
be applied by any other transaction on X.
Read Write
Read
Y N
Write
N N
Lock Manager
●
Lock Manager: Managing locks on data items.
● Lock table: Lock manager uses it to store the identify of transaction
locking a data item, the data item, lock mode and pointer to the next data
item locked.
●
One simple way to implement a lock table is through linked list.
●
The following code performs the read operation:
B: if LOCK (X) = “unlocked” then
begin LOCK (X) “read-locked”;
no_of_reads (X) 1;
end
else if LOCK (X) “read-locked” then
no_of_reads (X) no_of_reads (X) +1
else begin wait (until LOCK (X) = “unlocked” and
the lock manager wakes up the transaction);
go to B
end;
S hared / Exclus iv e Lock
end;
S hared / Exclus iv e Lock
The following code performs the unlock operation:
●
Lock upgrade: existing read lock to write lock
If Ti has a read-lock (X) and Tj has no read-lock (X) (i j) then
convert read-lock (X) to write-lock (X)
else
force Ti to wait until Tj unlocks X
●
Lock downgrade: existing write lock to read lock
Ti has a write-lock (X) (*no transaction can have any lock on X*)
convert write-lock (X) to read-lock (X)
Two-Phase Locking : Algorithm
●
Two Phases: (a) Locking (Growing) (b) Unlocking (Shrinking).
● Locking (Growing) Phase: A transaction applies locks (read or write) on
desired data items one at a time.
T’1 T’2
read_lock (Y); read_lock (X);
read_item (Y); read_item (X);
write_lock (X); write_lock (Y);
unlock (Y); unlock (X);
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
Two-Phase Locking : Algorithm
● Two-phase policy generates two locking algorithms (a) Basic and (b)
Conservative.
● Basic 2PL: Transaction locks data items incrementally. This may cause
deadlock which is dealt with.
Deadlock
T’1 T’2
read_lock (Y); T1 and T2 did follow two-phase
read_item (Y); policy but they are deadlock
read_lock (X);
read_item (Y);
write_lock (X);
(waits for X) write_lock (Y);
(waits for Y)
D e a d l o c k (T’1 a n d T’2)
Dealing with Deadlock
●
Deadlock Prevention Protocols
●
Deadlock Detection
●
Starvation
Dealing with Deadlock
●
Deadlock Prevention Protocols
● Lock all data items before transaction begins → Conservative 2PL – is a
deadlock prevention protocol but further limits concurrency.
●
Not generally used because of unrealistic assumptions or overhead.
●
What to do with a transaction involved in a deadlock?
• Should it be aborted ?
●
Issue: some transactions to be aborted and restarted needlessly
Dealing with Deadlock
●
Deadlock Detection
●
In this approach, deadlocks are allowed to happen.
●
Suitable for transactions that are short and locks only a few items.
● The scheduler maintains a wait-for-graph for detecting cycle. If a cycle
exists, then one transaction involved in the cycle is selected (victim) and
rolled-back.
● One of the transaction of the cycle is selected and rolled back – select
transactions that have not made many changes.
Dealing with Deadlock
●
This limitation is inherent in all priority based scheduling mechanisms.
●
Solution: using a first-come-first-served queue
References