0% found this document useful (0 votes)
4 views

Advanced DB-Chapter-Four Concurrency_control_Techniques (1)

Uploaded by

zedo1940
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Advanced DB-Chapter-Four Concurrency_control_Techniques (1)

Uploaded by

zedo1940
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

CHAPTER - 4

CONCURRENCY CONTROL TECHNIQUES


Various types of concurrency
control techniques
 Main
 Locking Techniques for Concurrency Control

 Concurrency Control Based ON Timestamp

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.

 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.

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.

 Locking: is an operation which secures



(a) permission to Read

(b) permission to Write a data item for a
transaction.

Notation :
:Li(X) –Transaction Ti requests a lock on database
item X.
 Unlocking: is an operation which removes these
permissions from the data item.

Notation :

Ui (X): Transaction Ti releases (“unlocks”) its lock
on database item X. Slide 3
Shared/Exclusive (or read /write)
locks
 There are three locking operations: read_lock(X),
write_lock(X), and unlock(X).

 A lock associated with an item X, LOCK(X), has


three possible states: "read-locked," "write-
locked," or "unlocked.“

 A read-locked item is also called share-locked,


because other transactions are allowed to read
the item, whereas a write-locked item is called
exclusive-locked, because a single transaction
exclusively holds the lock on the item.
5

 Data items can be locked in two modes :
 Shared mode: shared lock (X)

Used to read data item value.

Under shared lock, it is not possible to apply a
write operation.

More than one transaction can apply shared lock
on X for reading its value but no write lock can
be applied on X by any other transaction.

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.

 Consider the following transactions based on


shared/exclusive lock

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.

 Transaction T1 will wait for transaction T2 to give up


the lock, and similarly, transaction T2 will wait for
transaction T1 to give up the lock.
Slide 10
Deadlock …

Example of deadlock situation:


T’1 T’2
read_lock (Y);
read_item (Y);
read_lock (X);
read_item (X);
write_lock (X);
(waits for X) write_lock (Y);
(waits for Y)

 T’1 and T’2 enter deadlock


11
Deadlock …
T1 T2

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  

 Guaranteeing serializability by Two-Phase Locking


Protocol (2 PL)

 Deadlock prevention techniques

 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.

 This technique is used to order transactions based


on their time stamps.
 Order of transaction means ascending order of the
transaction creation.
 To determine the timestamp of the transaction, this
protocol uses system time, logical counter or unique
value.

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

Note: The time stamp of the 1st transaction is


always less that the time stamp of the next
transaction.

There is no deadlock issue in time stamp CCT.

Slide 18
Timestamp ordering (cont…)

Time stamp ordering algorithm associates
two-time stamp values (TS) with each
database item X

1. Read_TS(x) : the read time stamp 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.

2. Write_TS(X) : This the largest of all the timestamps


of transactions that have successfully written item x –
that is, write_TS(x) = TS(T), where T is the youngest
transaction that has written x successfully.

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

read_item(X) or a write_item(X) operation, the basic


algorithm compares the timestamp of T with
read_TS(X) and write_TS(X).

 The concurrency controller must check whether


conflicting operations violate the timestamp ordering or
not, as in the two cases provided in the next slide:

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)

2. Transaction T issues a read_item(X) operation:


a) If write_TS(X) > TS(T), then abort and roll-back T and
reject the operation. This is because a younger
transaction has already written the value before T had
the chance to read X;
b) If write_TS(X) < TS(T), then execute read_item(X) of T
and set read_TS(X) to the larger of TS(T) and the
current read_TS(X)
Slide 22
Timestamp ordering (cont…)
 Consider the following and check whether conflicting
operations violate the timestamp ordering or not.
 Given : WTS(X) = 0, RTS(X) = 0, TS(T1) = 1, TS(T2) =
2

T1 T2

R1(X)
W2(X)

R1(X)

Slide 23
Reading Assignment
 (due to shortage of time)

 Other Concurrency control techniques

3-Multi-Version Concurrency Control Techniques


4-Validation (Optimistic) Concurrency Control
Technique
5-Granularity of Data Items and Multiple Granularity
Locking
6-Using Locks for Concurrency Control in Indexes

Slide 24
I Thank You!!!

Slide 25

You might also like