Advanced DB-Chapter-Four Concurrency_control_Techniques (1)
Advanced DB-Chapter-Four Concurrency_control_Techniques (1)
Ordering
Others
Multi-Version Concurrency Control Techniques
Validation (Optimistic) Concurrency Control Technique
Granularity of Data Items and Multiple Granularity
Locking
Using Locks for Concurrency Control in Indexes
2
…
Purpose of Concurrency Control
To ensure Isolation property of concurrently
executing transactions.
To preserve database consistency.
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.
3
1- Concurrency control using Locks
A lock is a mechanism to control concurrent access
to a data item
A lock is a variable associated with a data item that
describes the status of the item with respect to
possible operations that can be applied to it.
Shared-Lock(A)
Read(A)
Unlock(A)
Slide 6
…
Exclusive mode: Write lock (X)
Used to read and write database items.
Only one write lock on data item can exist at any
time and no shared lock can be applied by any
other transaction on that same data item.
Exclusive-Lock(A)
Read(A)
A = A*0.5
Write(A)
Unlock(A)
7
…
When we use the shared/exclusive locking scheme,
the system must enforce the following rules:
1. A transaction must issue the operation read_lock(x)
before any read_item (x) operation is performed in T.
2. A transaction T must issue the operation write_lock(x)
before any write_item (x) operation is performed in T.
3. A transaction T must issue the operation
unlock_lock(x) after all read_item(x) and write_item
(x) operation are completed in T.
4. A transaction will not issue a read_lock(x) operation if
it already holds a write (exclusive) lock on item X.
5. A transaction will not issue a write_lock(x) operation
if it already holds read(shared) lock or
write(exclusive) lock on item x.
6. A transaction T will not issue an unlock(x) operation
unless it already holds a read(shared) lock or a
write(exclusive) lock on item x.
Slide 8
…
Using binary or read write locks in transactions as described earlier
by itself
does not guarantee serializability.
T1 T2 Result
Read_lock (Y); Read_lock (X); Initial values:
X=20; Y=30
read_item (Y); read_item (X);
Unlock (Y); Unlock (X); Result of serial
execution
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 9
Deadlock
Deadlock is the main issue in locking CC technique.
A deadlock is a situation in which two or more
transactions are waiting for one another to give up
locks.
• It occurs when each
transaction T, in a
set of two or more
transactions, is
waiting for an item
that is locked by
some other
transaction T’ in the
set.
read_lock (Y);
read_item (Y);
unlock (Y)
read_lock (X);
read_item (X);
unlock (X)
write_lock (X);
write_lock (Y);
There is no deadlock in this schedule since T1 unlocks y
and T2 unlocks x.
12
Read More About The Following
Starvation
13
Guaranteeing serializability by Two-Phase Locking
Protocol (2 PL)
A transaction is said to follow two phase locking protocol if all locking
operations (either read_lock or write_lock) precede the first unlock
operation in the transaction
This is a protocol which ensures conflict-serializable schedules.
Phase 1: Growing Phase
transaction may obtain locks
transaction may not release locks
Phase 2: Shrinking Phase
transaction may release locks
transaction may not obtain locks
It can be proved that the transactions can be serialized in the order of
their lock points (i.e. the point where a transaction acquired its final
lock).
Slide 14
Database Concurrency Control
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
Deadlock prevention and Starvation
Two schemes that prevent dead lock based on time stamp
Wait – die:
Wound - wait:
Another group of protocols that prevent deadlock do not
require timestamps
No waiting (NW)
Cautious waiting (CW)
Starvation
It occurs when a particular transaction consistently waits or
restarted and never gets a chance to proceed further.
16
2. Concurrency control based on
Timestamp ordering
Time stamp (TS): is a unique identifier created
by the DBMS to identify a transaction.
17
…
Timestamp based algorithm uses timestamp
to serialize the execution of concurrent
transactions.
A larger timestamp value indicates a more
recent event or operation
Time stamp of transaction Ti = start time of
transaction Ti
Slide 18
Timestamp ordering (cont…)
Time stamp ordering algorithm associates
two-time stamp values (TS) with each
database item X
Slide 19
Timestamp ordering (cont…)
Consider the following four transactions.
Given: TS(T1) = 10, TS(T2) = 20, TS(T3) = 30, and
TS(T4) = 40
Find the RTS and WTS of data item X.( initially both RTS
and WTS are 0)
T1 T2 T3 T4
R1(X)
W1(X)
R3(X)
R2(X)
W4(X)
Slide 20
Basic Timestamp Ordering protocol :
Whenever some transaction T tries to issue a
Slide 21
Timestamp ordering (cont…)
1. Transaction T issues a write_item(X) operation:
a) If read_TS(X) > TS(T) or if write_TS(X) > TS(T), then a
younger transaction has already read or written the
data item so abort and roll-back T and reject the
operation.
This is because a younger transaction has already read
or written the value before T had the chance to write
X;
b) If the condition in part (a) does not exist, then
execute write_item(X) of T and set write_TS(X) to TS(T)
T1 T2
R1(X)
W2(X)
R1(X)
Slide 23
Reading Assignment
(due to shortage of time)
Slide 24
I Thank You!!!
Slide 25