Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 25

Chapter 10

Concurrency Control Techniques

Objectives of concurrency
control
To enforce Isolation (through mutual
exclusion) among conflicting transactions.
To preserve database consistency through
consistency preserving execution of
transactions.
To resolve
conflicts.

read-write

and

write-write

Why is Concurrency Control


Needed?
The Lost Update Problem
T1
Read(P1)
P1=P1+10
0

T2

Read(P1)
P1=P1+5
0

Write(P1)
Write(P1)
Commit
Commit

Value of Data
Items
T1.P1300
T1.P1300+100=
400
T2.P1300
T2.P1300+50=3
50
T1 writes P1400
T2 writes P1350

This is incorr
value of P1.
Problem was
to overwritin
value of P1 b
Correct valu
450.

Why is Concurrency Control


Needed?
The Dirty Read (Temporary
Update) Problem
T1

T2

Read(P1)
P1=P1+10
0
Write(P1)
Read(P1)
P1=P1+5
0
Abort
Write(P1)
Commit

Value of Data
Items
T1.P1300
T1.P1300+100=
400
T1 writes P1400
T2.P1400
T2.P1400+50=4
50
T1 is aborted
(Undo)
T1 writes P1450

This is incorre
value of P1.
Problem was
to reading of
value of data
by T2. Correc
value is 350.

Why is Concurrency Control


Needed?
The Incorrect Summary
(Analysis) Problem
T1
Read(P1)
Sum=Sum+
P1

Read(P2)
Sum=Sum+
P2
Commit

T2
Read(P2)
P2=P2+50
Write(P2)

Commit

Value of Data Items


T1.P1300
T1.Sum0+300=30
0
T2.P2175
T2.P2175+50=225
T2 writes P2225
T1.P2225
T1.Sum300+225=525

i
m
u
ls
a
n
sh
i
f
h
The ect. T 2 w
T
r
cor ause o w
bec wed t T
t
allo tha i
item se in
u
to rect v
Cor
475

Why is Concurrency Control


Needed?
Unrepeatable Read Problem
T1
Read(P2)

Read(P2)
P2=P2-10
Write(P2)
Commit

T2

Value of Data
Items
T1.P215
Read(P2) T2.P215
P2=P2-10 T2.P25
Write(P2) T2 writes P25
Commit
T1.P25
T1.P25-10=-5
T1 writes P2 -5

The problem is due t


two different values
P2 got by T1 at two
operations. Value of
product inventory ca
not be negative.

The Concurrency Control


Protocols
Lock Based Protocols
Time Stamp Based Protocols
Validation Based Protocols
Multiversion Schemes

Lock Based Protocols


. Locks are of two kinds: Binary Locks
and Shared/Exclusive Locks
Lock conversion can be of two types:
Lock Upgrade and Lock Downgrade
This protocol is implemented in two
phase. Growing Phase and Shrinking
Phase.

First Schedule does not follow 2PL but Second


schedule follows 2PL.
T1

T2

Lock(x)
Read(x)
x=x+100
Write(x)
Unlock(x)
Lock(x)
x=x*2
Wtite(x)
Unlock(x)
Lock(y)
Read(y)
y=y*2
Write(y)
Unlock(y)
Lock(y)
Read(y)
y=y+100
Write(y)
Unlock(y)

Data Items Values

T1

x50
x150
T2 writes x150

Lock(x)
Read(x)
x=x+100
Write(x)
Lock(y)
Unlock(x)

y150
y250
T1 writes y250

Data Items Values


x50
x150
T1 writes x150

Lock(x)
Read(x)
x=x*2
Wtite(x)

x300
T2 writes x300
y=50
y=100
T2 writes y150

T2

Read(y)
y=y+100
Write(y)
Unlock(y)
Lock(y)
Unlock(x)
Read(y)
y=y*2
Write(y)
Unlock(y)

x150
x300
T2 writes x300
y=50
y=150
T1 writes y150

y150
y300
T2 writes y300

Lock Based Protocols


Two phase locking (2PL) can be
devided into following four
categories:
Basic 2PL
Conservative 2PL,
Strict 2PL, and
Rigorous 2PL.

Deadlock
Two schemes that prevent deadlock
are called wait-die and wound-wait.
Wait-Die Scheme
If TS(Ti)< TS(Tj) //Ti older tha Tj
then
Ti waits
else
T2
Wait

Die

T3

Ti dies
TS={5,10,15} for T2,T3 & T5 respectively

T4

Deadlock
Wound-Wait Scheme
if TS(Ti)< TS(Tj)
then
Tj is wounded (rolled back)
else
Ti waits
Aborted
&

T2

Rolledbak

Allowed to Wait

T3

T4

TS={5,10,15} for T2,T3 & T5 respectively

Deadlock Detection

S=R1(x), R2(y), W1(x), R2(x), R3(z), W3(z), R1(y), R3(x),


W1(y).
T1

T2

T3

Read-Lock(x)
Read(x)
Read-Lock(y)
Read(y)
Write-Lock(x)
Write(x)
Read-Lock(x)-wait for
T1
Read-Lock(z)
Read(z)
Write-Lock(z)
Write(z)
Read-Lock(y)
Read(y)
Write-Lock(y)-wait
for T2

Read-Lock(x)-wait
for T1

Deadlock Detection
This situation can be represented in
wait-for as below.Since the graph
contains cycle T1T2T1, the given
schedule suffers from deadlock
problem
T2

T1
T3

Recovery from Deadlocks


Once deadlock is detected we need
some
mechanisms
to
resolve
deadlock situation.
Victim Section
Timeout Mechanisms

Starvation

Time Stamp based Concurrency


Control
To implement this scheme we must
associate with each data item x to the
timestamp values.
WriteTS(x):- It denotes the largest timestamp of any transaction that execute
write(x) operation successfully.
ReadTS(x):- It denotes the largest timestamp of any transaction that executed
read(x)
operation
successfully
successfully.

Basic TO (Timestamp Ordering)


Protocol
Case I: Transaction T Issues Write(x) Operation
If ReadTS(x) > TS(T) or if WriteTS (x) > TS(T), then
abort and roll back T and reject the Operation..
If the above condition is not satisfed then execute
he Write(x) operation and set WriteTS(x) to TS(T).
Case II: Transaction T Issues Read(x) Operation
If WriteTS (x) >TS(T), then abort and rollback Tand
reject the operation.
If writeTS(x) < = TS (T), then execute the Read(x)
operation of T and set ReadTS(x) to the larger to
TS(T) and the current ReadTS(x).

Time Stamp based Concurrency


Control
Example: Assume timestamps of T1 and T2 is 100
and 110 respecitively and intial value of x is 500.
T1
Read(x)

T2
Read(x)

Timestamps
ReadTS(x)100
ReadTS(x)110

x=x+200
x=x+200
Write(x)
Write(x)
Abort T1

WriteTS(x)110
WriteTS(x)>TS(T1) and
hence Timestamp order
violated

In this example if transaction T1 is not aborted it


will suffer from lost update problem and will make
fnal value of x=500 rather than 700.

Strict Timestamp
Ordering

Strict Timestamp ordering is a variation of basic timestamp


ordering.
In this variation, a transaction T, that issues a Read(x) or
Write(x) operation such that TS(T) > WriteTS(x) has its read
or write operation delayed until the transaction T1 that
wrote the value of x has committed or aborted.
T1

T2

Read(x)
x=x+20
0
Write(x)

Read(x)
x=x+20
0
Write(x)

Timestamps
ReadTS(x)100
ReadTS(x)110
WriteTS(x)100
TS(T2)>WriteTS(x) ,
therefore T2 wiats until
T1 is terminated

Thomas Write Rule


TWR is modifcation of Basic TO
Read operation remain unchanged but write operations are
different from the basic TO. If a transaction T issues a write(x)
operation
If TS (T) <ReadTS(x), abort and roll-back transaction T and
reject the operation
If TS (T)<WriteTS(x), ignore the write operation and continue
execution.
Otherwise
the Write(x) operation.
T1 system
T2 performs
Timestamps
Read(x)
Write(x)
Write(x)

ReadTS(x)100
WriteTS(x)110
TS(T1)<WriteTS(x) ,
therefore from TWR
Write(x) operation must
be ignored.

Multiversion
Concurrency Control

In multi version database systems, each write


operation on data item say x creates a new version
of x. When a Read(x) operation is issued, the system
selects one of the versions of x to read.
The concurrency control scheme must ensure that
the selection of the version to be read is done in a
manner that ensures serializability. There are two
multi version schemes
Multiversion techniques based on Timestamp ordering
Multiversion Techniques based on Two-phase locking

An obvious drawback of multiversion techniques is


that more storage is needed to maintain multiple
versions of the database items. However, older
versions may have to be maintained anywayfor

Multiversion Technique Based


on TO

For each version the value of the version xk and the following
two timestamps are kept.
WriteTS(xk) is the timestamp of the transaction that created version
xk of the data item.
ReadTS(xk) is largest timestamp of any transaction that successfully
read version xk of the data item.

Let xk denote the version of x whose timestamp is the largest


write timestamp less than or equal to TS(Ti). This means xk is
the version of data item x that is written by youngest
transaction that is older than transaction Ti.
If transaction Ti issues a Read(x) operation, then the value
returned is the content of version xk
If transaction Ti issues a Write(x) operation, and if
TS(Ti)<ReadTS(xk), then
transaction Ti is rolled back.
Otherwise if TS(Ti)=WriteTS(xk) the contents of xk are over
written, otherwise a new version of x is created.

Validation (Optimistic)
Concurrency Control Schemes
Lock Based and TS Based Protocols have following
overheads:
Lock Based Protocols: Needs to lock items before
performing read or write operation.
Timestamp Based Protocols needs to checks read TS
and write TS before performing read/ write operation
Validation Concurrency Control Scheme Works in three
phases for this concurrency control protocol are:
Read phase: Reads committed and updates only local
copies.
Validation phase:Ensure serializability.
Write phase:If the validation phase is successful updates
adatabase otherwise discarded and the transaction is
restarted.

Validation (Optimistic)
Concurrency Control Schemes
Suppose Ti is in its validation phase, and Tj is any
transaction that has committed or is also in its validation
phase, then one of three conditions must be true for
serializability to hold. If none of these conditions hold, Ti is
aborted
Tj
Read

Validation

Write

Ti

Tj

Ti

Read

Validation

Read

B
Tj

B
Ti

Validation

Write

Ti does not read anything that Tj


writes

Write

Validation

Read

Write

Read

Validation

Read

Validation

Write

Ti does not read or write


anything that Tj writes.

Write

Multiple Granularity
Databas
e
File

File
File

Page

Page

Page

Record

Record
Record

Field

Field

You might also like