Sunteți pe pagina 1din 6

Concurrency Control-:

It ensures serializibilty. These are the protocols through which it can guarantee
serializibilty. There are three types of protocols. These are:-
1. Lock-Based Protocols
. Ti!esta!p-Based Protocols
". #alidation-Based Protocols
Lock Based Protoclol:-
1. $ lock is a !echanis! to control concurrent access to a data ite!
. %ata ite!s can be locked in two !odes
1. exclusive (X) mode. %ata ite! can be both read as well as
written. &-lock is re'uested using lock-X instruction.
. shared (S) mode. %ata ite! can only be read. (-lock is
re'uested using lock-S instruction.
Lock re'uests are !ade to concurrency-control !anager. Transaction can proceed
only after re'uest is granted.
$ny nu!ber of transactions can hold shared locks on an ite!) but if any
transaction holds an e*clusi+e on the ite! no other transaction !ay hold any lock
on the ite!.
If a lock cannot be granted) the re'uesting transaction is !ade to wait till all
inco!patible locks held by other transactions ha+e been released. The lock is
then granted.
,*a!ple of a transaction perfor!ing locking:
T2: lock-S(A)-
read (A)-
unlock(A)-
lock-S(B)-
read (B)-
unlock(B)-
display(A+B)
This techni'ues works in two phase so it is called two phase locking
1. .rowing or e*panding phase
a. transaction !ay obtain locks
b. transaction !ay not release locks
. (hrinking phase
a. transaction !ay release locks
b. transaction !ay not obtain locks
This techni'ue assures serializability. It can be pro+ed that the transactions can be
serialized in the order of their lock points /i.e. the point where a transaction ac'uired its
final lock0.
Disadvantages:-
1. %oes not ensure freedo! fro! deadlock
. 1ascading roll-back is possible under two-phase locking
". 2+erhead of !aintaining of lock table.
To a+oid cascading rollbacks Strict two phase locking is used. 3ere a transaction !ust
hold all its e*clusi+e locks till it co!!its4aborts.
Rigorous two-phase locking is e+en stricter: here all locks are held till co!!it4abort. In
this protocol transactions can be serialized in the order in which they co!!it
Tiestap-Based Protocols
,ach transaction is issued a ti!esta!p when it enters the syste!. If an old
transaction Ti has ti!e-sta!p T(/Ti0) a new transaction Tj is assigned ti!e-sta!p
T(/Tj0 such that T(/Ti0 5T(/Tj0.
The protocol !anages concurrent e*ecution such that the ti!e-sta!ps deter!ine
the serializability order.
In order to assure such beha+ior) the protocol !aintains for each data Q two
ti!esta!p +alues:
!-tiestap/Q0 is the largest ti!e-sta!p of any transaction that
e*ecuted write/Q0 successfully.
R-tiestap/Q0 is the largest ti!e-sta!p of any transaction that e*ecuted
read/Q0 successfully.
The ti!esta!p ordering protocol ensures that any conflicting read and write operations
are e*ecuted in ti!esta!p order.
(uppose a transaction Ti issues a read/Q0
1. If T(/Ti0 !-ti!esta!p/Q0) then Ti needs to read a +alue of Q
that was already o+erwritten. 3ence) the read operation is
re6ected) and Ti is rolled back.
. If T(/Ti0 !-ti!esta!p/Q0) then the read operation is
e*ecuted) and 7-ti!esta!p/Q0 is set to the !a*i!u! of 7-
ti!esta!p/Q0 and T(/Ti0.
(uppose that transaction Ti issues write/Q0.
1. If T(/Ti0 5 7-ti!esta!p/Q0) then the +alue of Q that Ti is producing was needed
pre+iously) and the syste! assu!ed that that +alue would ne+er be produced. 3ence)
the write operation is re6ected) and Ti is rolled back.

. If T(/Ti0 5 8-ti!esta!p/Q0) then Ti is atte!pting to write an obsolete +alue of Q.
3ence) this write operation is re6ected) and Ti is rolled back.
". 2therwise) the write operation is e*ecuted) and 8-ti!esta!p/Q0 is set to T(/Ti0.
Ti!esta!p protocol ensures freedo! fro! deadlock as no transaction e+er
waits.
But the schedule !ay not be cascade-free) and !ay not e+en be reco+erable.
"alidation-Based Protocol
,*ecution of transaction Ti is done in three phases.
#$ Read and e%ecution phase: Transaction Ti writes only to
te!porary local +ariables
&$ "alidation phase: Transaction Ti perfor!s a 99+alidation test::
to deter!ine if local +ariables can be written without +iolating
serializability.
'$ !rite phase: If Ti is +alidated) the updates are applied to the
database- otherwise) Ti is rolled back.
The three phases of concurrently e*ecuting transactions can be interlea+ed) but each
transaction !ust go through the three phases in that order.
It is called as optiistic concurrency control since transaction e*ecutes fully in the
hope that all will go well during +alidation
1. ,ach transaction Ti has " ti!esta!ps
Start/Ti0 : the ti!e when Ti started its e*ecution
"alidation/Ti0: the ti!e when Ti entered its +alidation phase
(inish/Ti0 : the ti!e when Ti finished its write phase
. (erializability order is deter!ined by ti!esta!p gi+en at +alidation ti!e) to
increase concurrency. Thus TS/Ti0 is gi+en the +alue of "alidation/Ti0.
". This protocol is useful and gi+es greater degree of concurrency if probability of
conflicts is low. That is because the serializability order is not pre-decided and
relati+ely less transactions will ha+e to be rolled back.
,*a!ple
T1 T

read(B)
read(B)
B:- B-50
read(A)
A:- A+50
read(A)
(validate)
display (A+B)
(validate)
write (B)
write (A)
)ultiple *ranularity
1. $llow data ite!s to be of +arious sizes and define a hierarchy of data
granularities) where the s!all granularities are nested within larger ones
. 1an be represented graphically as a tree /but don:t confuse with tree-locking
protocol0
". 8hen a transaction locks a node in the tree explicitly) it implicitly locks all the
node:s descendents in the sa!e !ode.
;. .ranularity of locking /le+el in tree where locking is done0:
fie !raularity /lower in tree0: high concurrency) high locking o+erhead
coarse !raularity /higher in tree0: low locking o+erhead) low
concurrency
"xample

The highest le+el in the e*a!ple hierarchy is the entire database. The le+els below are of
type area) file and record in that order.
+ntention Lock )odes
In addition to ( and & lock !odes) there are three additional lock !odes with
!ultiple granularity:
intention-shared /I(0: indicates e*plicit locking at a lower le+el of the tree
but only with shared locks.
intention-exclusive /I&0: indicates e*plicit locking at a lower le+el with
e*clusi+e or shared locks
shared and intention-exclusive /(I&0: the subtree rooted by that node is
locked e*plicitly in shared !ode and e*plicit locking is being done at a
lower le+el with e*clusi+e-!ode locks.
intention locks allow a higher le+el node to be locked in ( or & !ode without ha+ing
to check all descendent nodes.
)ultiple *ranularity Locking Schee
Transaction Ti can lock a node Q) using the following rules:
1. The lock co!patibility !atri* !ust be obser+ed.
. The root of the tree !ust be locked first) and !ay be locked in
any !ode.
". $ node Q can be locked by Ti in ( or I( !ode only if the parent
of Q is currently locked by Ti in either I& or I(
!ode.
;. $ node Q can be locked by Ti in &) (I&) or I& !ode only if the
parent of Q is currently locked by Ti in either I&
or (I& !ode.
<. Ti can lock a node only if it has not pre+iously unlocked any node
/that is) Ti is two-phase0.
=. Ti can unlock a node Q only if none of the children of Q are
currently locked by Ti.
2bser+e that locks are ac'uired in root-to-leaf order)
whereas they are released in leaf-to-root order.
)ultiversion Schees
1. >ulti+ersion sche!es keep old +ersions of data ite! to increase concurrency.
>ulti+ersion Ti!esta!p 2rdering
>ulti+ersion Two-Phase Locking
. ,ach successful write results in the creation of a new +ersion of the data ite!
written.
". ?se ti!esta!ps to label +ersions.
;. 8hen a read/Q0 operation is issued) select an appropriate +ersion of Q based
on the ti!esta!p of the transaction) and return the +alue of the selected
+ersion.
<. reads ne+er ha+e to wait as an appropriate +ersion is returned i!!ediately.
=. ,ach data ite! Q has a se'uence of +ersions 5Q#$ Q2$....$ Qm@. ,ach +ersion
Q% contains three data fields:
Content -- the +alue of +ersion Q%.
!-tiestap/Q%0 -- ti!esta!p of the transaction that created /wrote0
+ersion Ak
R-tiestap/Q%0 -- largest ti!esta!p of a transaction that successfully
read +ersion Ak
B. when a transaction Ti creates a new +ersion Q% of Q) Ak:s 8-ti!esta!p and
7-ti!esta!p are initialized to T(/Ti0.
C. 7-ti!esta!p of Q% is updated whene+er a transaction Tj reads Q%) and T(/Tj0
@ 7-ti!esta!p/Q%0
D. The !ulti+ersion ti!esta!p sche!e presented ne*t ensures serializability.
1E. (uppose that transaction Ti issues a read/Q0 or write/Q0 operation. Let Q%
denote the +ersion of Q whose write ti!esta!p is the largest write ti!esta!p
less than or e'ual to T(/Ti0.
If transaction Ti issues a read/Q0) then the +alue returned is the
content of +ersion Ak.

If transaction Ti issues a write/Q0) and if T(/Ti0 & 7-
ti!esta!p/Q%0) then transaction Ti is rolled back. 2therwise) if T(/Ti0
8-ti!esta!p/Q%0) the contents of Q% are o+erwritten) otherwise a new
+ersion of Q is created.
11. 7eads always succeed- a write by Ti is re6ected if so!e other transaction Tj
that /in the serialization order defined by the ti!esta!p +alues0 should read
Ti:s write) has already read a +ersion created by a transaction older than Ti.
1. %ifferentiates between read-only transactions and update transactions
1". 'pdate trasactios ac'uire read and write locks) and hold all locks up to the
end of the transaction. That is) update transactions follow rigorous two-phase
locking.
,ach successful write results in the creation of a new +ersion of the data
ite! written.
each +ersion of a data ite! has a single ti!esta!p whose +alue is
obtained fro! a counter ts-counter that is incre!ented during co!!it
processing.
1;. (ead)oly trasactios are assigned a ti!esta!p by reading the current +alue
of ts-counter before they start e*ecution- they follow the !ulti+ersion
ti!esta!p-ordering protocol for perfor!ing reads.
1<. 8hen an update transaction wants to read a data ite!) it obtains a shared lock
on it) and reads the latest +ersion.
1=. 8hen it wants to write an ite!) it obtains & lock on- it then creates a new
+ersion of the ite! and sets this +ersion:s ti!esta!p to .
1B. 8hen update transaction Ti co!pletes) co!!it processing occurs:
Ti sets ti!esta!p on the +ersions it has created to ts-counter F 1
Ti incre!ents ts-counter by 1
1C. 7ead-only transactions that start after Ti incre!ents ts-counter will see the
+alues updated by Ti.
1D. 7ead-only transactions that start before Ti incre!ents the
ts-counter will see the +alue before the updates by Ti.
E. 2nly serializable schedules are produced.

S-ar putea să vă placă și