Chapter 3 Concurrency Control Techniques (1) (1)
Chapter 3 Concurrency Control Techniques (1) (1)
Chapter 3
Concurrency Control Techniques
• Purpose of Concurrency Control
▫ To ensure Isolation property of concurrently
executing transactions
▫ To preserve database consistency
▫ To resolve read-write and write-write conflicts
• Example:
▫ In concurrent execution environment,
if T1 conflicts with T2 over a data item A, then
the existing concurrency controller decides if T1
or T2 should get A and which transaction should
be rolled-back or waits
Slide 2
Lock conversion
• Sometimes, it is desirable to relax condition 4 and 5 in
the coding list in order to allow lock conversions. That is
:
▫ Under certain conditions, a transaction that already
holds a lock on item X is allowed to convert the lock
from one lock state to another.
▫ For example, it is possible for a transaction T to issue
a read_lock (X) and then later on to upgrade the lock
by issuing a write_lock(x) operation
▫ If T is the only transaction holding a read lock on x at
the time it issues the write_lock (x) operation, the lock
can be upgraded ;otherwise, the transaction must
wait.
▫ It is also possible for a transaction T to issue a
write_lock(X) and then later on to downgrade the lock
by issuing a read_lock(X) operation
Slide 7
• Lock compatibility
▫ A transaction may be granted a lock on an item if the
requested lock is compatible with locks already held
on the item by other transactions
▫ Any number of transactions can hold shared locks on
an item,
But if any transaction holds an exclusive lock on the item
no other transaction may hold any lock on the item.
▫ If a lock cannot be granted, the requesting
transaction will be made to wait untill all
incompatible locks held by other transactions have
been released
Lock compatibility matrix
Slide 9
T1 T2 Result
read_lock (Y); read_lock (X); Initial values:
X=20; Y=30
read_item (Y); read_item (X); Result of serial
execution
unlock (Y); unlock (X); T1 followed by T2
write_lock (X); Write_lock (Y); X=50, Y=80.
read_item (X); read_item (Y); Result of
serial execution
X:=X+Y; Y:=X+Y; T2 followed by T1
write_item (X); write_item (Y); X=70, Y=50
unlock (X); unlock (Y);
Slide 12
T’1 T’2
read_lock (Y); read_lock (X); T’1 and T’2 follow two-
phase
read_item (Y);read_item (X); policy but they are
subject to
write_lock (X); Write_lock (Y); deadlock
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);
Slide
15
T1 T2
read_lock (Y);
read_item (Y);
unlock (y)
read_lock (X);
read_item (X);
unlock (X)
write_lock (X);
write_lock (Y);
• Deadlock prevention
▫ Two schemes that prevent dead lock based on time
stamp includes wait –die and wound-wait
▫ Suppose that transaction Ti 100 tries to lock an item
X but is not able to do so because X is locked by some
other transaction Tj 90 with a conflicting lock. The
rules followed by these two schemes are as follows:
Wait – die: If TS(Ti) < TS(Tj) i.e Ti is older than Tj, then
Ti is allowed to wait ; other wise, abort Ti (Ti dies) and
restart it later with the same time stamp
Wound - wait: If TS(Ti) < TS(Tj), abort Tj (Ti wounds Tj)
and restart it later with the same timestamp; other wise
Ti is allowed to wait.
Slide
21
• Timestamp (TS)
▫ Timestamp is a unique identifier created by the
DBMS to identify a transaction
▫ A larger timestamp value indicates a more recent
event or operation
▫ Timestamp based algorithm uses timestamp to
serialize the execution of concurrent transactions
▫ Timestamp ordering algorithm associates two
timestamp values (TS) with each database item X
1. Read_TS(x) : the read timestamp of x: This is
the largest timestamp among all the timestamps
of transactions that have successfully read item
x
Read_TS(X)=TS(T) where T is the youngest
transaction that has read X successfully
Slide
24
Multiversion techniques
(cont…)
• Assume X1, X2, …, Xn are the version of a
data item X created by a write operation of
transactions.
With each Xi a read_TS (read timestamp) and a
write_TS (write timestamp) are associated.
▫ read_TS(Xi): The read timestamp of Xi is
the largest of all the timestamps of
transactions that have successfully read
version Xi.
▫ write_TS(Xi): The write timestamp of Xi
that wrote the value of version Xi.
Slide
28
Multiversion techniques
(cont…)
▫ Whenever a transaction T is allowed to
execute a write_item(X) operation, a new
version Xk+1 of item X is created with both
the write_TS(Xk+1) and read_TS(Xk+1) set to
TS(T).
▫ Correspondingly, when a transaction is
allowed to read the value of xi, the value of
read_TS(xi) is set to the larger of the
current read_TS(xi) and TS(T)
Slide
29
r111... r11j r12...r12j r1n1..r1nj r211... r21k r221 ... r21k r2m1 ... r2mk
Slide
34
Intention-shared (IS)
Intention-exclusive (IX)
IS IX S SIX X Shared-intention-exclusive (SIX)
IS yes yes yes yes no
IX yes yes no no no
S yes no yes no no
SIX yes no no no no
X no no no no no
Slide
36
(Cont…)
• The set of rules (multiple granularity locking, MGL)
which must be followed for producing serializable
schedule are:
1. The lock compatibility must adhered to.
2. The root of the tree must be locked first, in any
mode..
3. A node N can be locked by a transaction T in S
or IX mode only if the parent node is already
locked by T in either IS or IX mode.
4. A node N can be locked by T in X, IX, or SIX
mode only if the parent of N is already locked by
T in either IX or SIX mode.
5. T can lock a node only if it has not unlocked any
node (to enforce 2PL policy).
6. T can unlock a node, N, only if none of the
children of N are currently locked by T.
Slide
37
Cont…
unlock(r111)
unlock(p11)
unlock(f1)
unlock(db)
unlock (r111j)
unlock (p11)
unlock (f1)
unlock(f2)
unlock(db)
Slide
40
Cont…