Sunteți pe pagina 1din 7

Concurrency Arch|tecture SCL Server 2000

When many people aLLempL Lo modlfy daLa ln a daLabase aL Lhe same Llme a sysLem of
conLrols musL be lmplemenLed so LhaL modlflcaLlons made by one person do noL adversely
affecL Lhose of anoLher person 1hls ls called concurrency conLrol
Concurrency conLrol Lheory has Lwo classlflcaLlons for Lhe meLhods of lnsLlLuLlng concurrency
conLrol
O 9esslmlsLlc concurrency conLrol
A sysLem of locks prevenLs users from modlfylng daLa ln a way LhaL affecLs oLher users
AfLer a user performs an acLlon LhaL causes a lock Lo be applled oLher users cannoL
perform acLlons LhaL would confllcL wlLh Lhe lock unLll Lhe owner releases lL 1hls ls
called pesslmlsLlc conLrol because lL ls malnly used ln envlronmenLs where Lhere ls hlgh
conLenLlon for daLa where Lhe cosL of proLecLlng daLa wlLh locks ls less Lhan Lhe cosL of
rolllng back LransacLlons lf concurrency confllcLs occur
O pLlmlsLlc concurrency conLrol
ln opLlmlsLlc concurrency conLrol users do noL lock daLa when Lhey read lL When an
updaLe ls performed Lhe sysLem checks Lo see lf anoLher user changed Lhe daLa afLer lL
was read lf anoLher user updaLed Lhe daLa an error ls ralsed 1yplcally Lhe user
recelvlng Lhe error rolls back Lhe LransacLlon and sLarLs over 1hls ls called opLlmlsLlc
because lL ls malnly used ln envlronmenLs where Lhere ls low conLenLlon for daLa and
where Lhe cosL of occaslonally rolllng back a LransacLlon ouLwelghs Lhe cosLs of locklng
daLa when read
MlcrosofL SCL Server` 2000 supporLs a wlde range of opLlmlsLlc and pesslmlsLlc concurrency
conLrol mechanlsms users speclfy Lhe Lype of concurrency conLrol by speclfylng
O A LransacLlon lsolaLlon level for a connecLlon
O Concurrency opLlons on cursors
ock|ng Arch|tecture S Server 2000
A lock ls an ob[ecL used by sofLware Lo lndlcaLe LhaL a user has some dependency on a resource
1he sofLware does noL allow oLher users Lo perform operaLlons on Lhe resource LhaL would
adversely affecL Lhe dependencles of Lhe user ownlng Lhe lock Locks are managed lnLernally by
sysLem sofLware and are acqulred and released based on acLlons Laken by Lhe user
MlcrosofL SCL Server` 2000 uses locks Lo lmplemenL pesslmlsLlc concurrency conLrol among
mulLlple users performlng modlflcaLlons ln a daLabase aL Lhe same Llme 8y defaulL SCL Server
manages boLh LransacLlons and locks on a per connecLlon basls lor example lf an appllcaLlon
opens Lwo SCL Server connecLlons locks acqulred by one connecLlon cannoL be shared wlLh Lhe
oLher connecLlon nelLher connecLlon can acqulre locks LhaL would confllcL wlLh locks held by
Lhe oLher connecLlon nly bound connecLlons are noL affecLed by Lhls rule
SCL Server locks are applled aL varlous levels of granularlLy ln Lhe daLabase Locks can be
acqulred on rows pages keys ranges of keys lndexes Lables or daLabases SCL Server
dynamlcally deLermlnes Lhe approprlaLe level aL whlch Lo place locks for each 1ransacLSCL
sLaLemenL 1he level aL whlch locks are acqulred can vary for dlfferenL ob[ecLs referenced by
Lhe same query for example one Lable may be very small and have a Lable lock applled whlle
anoLher larger Lable may have row locks applled 1he level aL whlch locks are applled does noL
have Lo be speclfled by users and needs no conflguraLlon by admlnlsLraLors Lach lnsLance of
SCL Server ensures LhaL locks granLed aL one level of granularlLy respecL locks granLed aL
anoLher level lor example lf DerA aLLempLs Lo acqulre a share lock on a row Lhe lnsLance of
SCL Server also aLLempLs Lo acqulre lnLenL share locks on Lhe page and Lhe Lable lf Der8 has
an excluslve lock aL Lhe page or Lable level DerA ls blocked from acqulrlng locks unLll Lhe lock
held by Der8 ls freed
1here are several lock modes shared updaLe excluslve lnLenL and schema 1he lock mode
lndlcaLes Lhe level of dependency Lhe connecLlon has on Lhe locked ob[ecL SCL Server conLrols
how Lhe lock modes lnLeracL lor example an excluslve lock cannoL be obLalned lf oLher
connecLlons hold shared locks on Lhe resource
Locks are held for Lhe lengLh of Llme needed Lo proLecL Lhe resource aL Lhe level requesLed
O 1he duraLlon of share locks used Lo proLecL reads depends on Lhe LransacLlon lsolaLlon
levels AL Lhe defaulL LransacLlon lsolaLlon level of 8LAu CMMl11Lu a share lock ls
held only as long as lL Lakes Lo read a page ln scans Lhe lock ls held unLll a lock ls
acqulred on Lhe nexL page ln a scan lf Lhe PLuLCk hlnL ls speclfled or Lhe LransacLlon
lsolaLlon level ls seL Lo elLher 8L9LA1A8LL 8LAu or SL8lALlZA8LL Lhe locks are held Lo
Lhe end of Lhe LransacLlon
O uependlng on Lhe concurrency opLlons seL for a cursor Lhe cursor may acqulre shared
mode scroll locks Lo proLecL feLches When scroll locks are needed Lhey are held unLll
Lhe nexL feLch or Lhe closlng of Lhe cursor whlchever happens flrsL lf PLuLCk ls
speclfled however Lhe scroll locks are held unLll Lhe end of Lhe LransacLlon
O Lxcluslve locks used Lo proLecL updaLes are held unLll Lhe end of Lhe LransacLlon
lf a connecLlon aLLempLs Lo acqulre a lock LhaL confllcLs wlLh a lock held by anoLher connecLlon
Lhe connecLlon aLLempLlng Lo acqulre Lhe lock ls blocked unLll
O 1he confllcLlng lock ls freed and Lhe connecLlon acqulres Lhe lock lL requesLed
O 1he LlmeouL lnLerval for Lhe connecLlon explres 8y defaulL Lhere ls no LlmeouL
lnLerval buL some appllcaLlons seL a LlmeouL lnLerval Lo prevenL an lndeflnlLe walL
lf several connecLlons become blocked walLlng for confllcLlng locks on a slngle resource Lhe
locks are granLed on a flrsLcome flrsLserve basls as Lhe precedlng connecLlons free Lhelr locks
SCL Server has an algorlLhm Lo deLecL deadlocks a condlLlon where Lwo connecLlons have
blocked each oLher lf an lnsLance of SCL Server deLecLs a deadlock lL wlll LermlnaLe one
LransacLlon allowlng Lhe oLher Lo conLlnue
SCL Server may dynamlcally escalaLe or deescalaLe Lhe granularlLy or Lype of locks lor
example lf an updaLe acqulres a large number of row locks and has locked a slgnlflcanL
percenLage of a Lable Lhe row locks are escalaLed Lo a Lable lock lf a Lable lock ls acqulred Lhe
row locks are released SCL Server 2000 rarely needs Lo escalaLe locks Lhe query opLlmlzer
usually chooses Lhe correcL lock granularlLy aL Lhe Llme Lhe execuLlon plan ls complled
|tr|buted 1ranact|on Arch|tecture S Server 2000


|tr|buted 1ranact|on
9rocesslng LransacLlons ls easy as long as Lhe LransacLlon only lnvolves a slngle daLabase
unforLunaLely mosL enLerprlses donL sLore all of Lhelr lmporLanL lnformaLlon ln a slngle
daLabase ln many cases Lhe lnformaLlon wlll be spread ouL over many daLabases An onllne
reLaller for example mlghL have a daLabase of cusLomer accounL lnformaLlon LhaL ls sLored ln
an racle daLabase aL Lhe corporaLe offlce 8uL order fulflllmenL lnformaLlon may be sLored ln a
SCL Server daLabase aL Lhe warehouse of a parLner company When a cusLomer places an order
wlLh Lhe onllne reLaller Lhe cusLomers accounL should be deblLed ln Lhe daLabase aL Lhe
corporaLe offlce and an order record should be wrlLLen Lo Lhe parLner companys SCL Server
daLabase 1he onllne reLallers repuLaLlon would be rulned lf Lhey rouLlnely bllled cusLomers for
producLs LhaL were never shlpped So Lo keep Lhls from happenlng updaLes need Lo be
performed on boLh of Lhese daLabases as a slngle LransacLlon lf elLher operaLlon falls Lhen
each resource manager (daLabase) wlll roll back lLs parL of Lhe LransacLlon
1hls Lype of LransacLlon ls called a dlsLrlbuLed LransacLlon A dlsLrlbuLed LransacLlon ls a
LransacLlon LhaL lnvolves mulLlple lndependenL resource managers ln Lhls case Lhe racle
daLabase aL Lhe corporaLe offlce and Lhe SCL Server daLabase aL Lhe parLners warehouse
LlLher all of Lhe lnvolved resource managers wlll commlL Lhelr parL of Lhe LransacLlon or lf any
of Lhe resource managers are unable Lo commlL Lhelr parL of Lhe LransacLlon Lhen boLh
resource managers wlll roll back Lhelr updaLes
1wophae Comm|t rotoco|
1he key Lo lmplemenLlng dlsLrlbuLed LransacLlons ls Lhe Lwophase commlL proLocol ln Lhls
proLocol Lhe acLlvlLy of one or more resource managers ls conLrolled by a separaLe plece of
sofLware called a LransacLlon coordlnaLor 1he flve sLeps ln Lhls proLocol are as follows
1 An appllcaLlon lnvokes Lhe commlL meLhod ln Lhe LransacLlon coordlnaLor
2 1he LransacLlon coordlnaLor conLacLs each resource manager lnvolved ln Lhe LransacLlon
and Lells lL Lo prepare Lo commlL Lhe LransacLlon (Lhls ls Lhe beglnnlng of phase one)
3 ln order Lo respond ln Lhe afflrmaLlve Lo Lhe prepare phase a resource manager musL
puL lLself lnLo a sLaLe where lL can guaranLee LhaL lL wlll commlL Lhe LransacLlon lf Lold Lo
do so or roll back Lhe LransacLlon lf Lold Lo do so MosL resource managers wlll wrlLe a
log wlLh lLs lnLended changes Lo durable sLorage lf Lhe resource manager ls unable Lo
prepare Lhe LransacLlon lL wlll reply Lo Lhe LransacLlon coordlnaLor wlLh a negaLlve
response
4 1he LransacLlon coordlnaLor collecLs all of Lhe responses from Lhe resource managers
3 ln phase Lwo Lhe LransacLlon coordlnaLor lnforms each resource manager of Lhe
ouLcome of Lhe LransacLlon lf any of Lhe resource managers responded ln Lhe negaLlve
Lhen Lhe LransacLlon coordlnaLor sends a rollback command Lo all of Lhe resource
managers lnvolved ln Lhe LransacLlon lf Lhey all responded ln Lhe afflrmaLlve Lhen Lhe
LransacLlon coordlnaLor lnsLrucLs all Lhe resource managers Lo commlL Lhe LransacLlon
nce Lhe resource managers are Lold Lo commlL Lhe LransacLlon cannoL fall afLer LhaL
8y respondlng ln Lhe afflrmaLlve Lo phase one each resource manager was guaranLeelng
LhaL Lhe LransacLlon would noL fall lf lL were Lold Lo commlL laLer
1he Arch|tecture of a keource Manager
now LhaL you have a baslc lnLroducLlon Lo Lhe fundamenLal concepLs of LransacLlon
processlng leLs brlng Lhls dlscusslon back ln order Lo work wlLh Lhe Lwophase commlL
proLocol a resource manager musL perform lLs operaLlons ln Lwo phases llrsL Lhe resource
manager musL place Lhe resources LhaL lL conLrols ln a sLaLe where lL can guaranLee elLher LhaL
changes can be made permanenL lf Lhe LransacLlon commlLs or undo Lhe changes lf Lhe
LransacLlon ls rolled back Second Lhe resource manager musL walL for Lhe LransacLlon
coordlnaLor Lo lnform lL of Lhe ouLcome of Lhe LransacLlon
MosL resource managers lmplemenL Lhls loglc wlLh Lhe help of a durable log flle ln phase
one Lhe resource manager wrlLes a record of all Lhe work lL lnLends Lo do Lo Lhe durable log
flle Lhen performs whaLever work ls necessary Lo place lLself ln a sLaLe LhaL guaranLees Lhe
ouLcome of Lhe LransacLlon AfLer lL performs Lhese sLeps Lhe resource manager can respond ln
Lhe afflrmaLlve Lo phase one
WhaL happens ln Lhe second phase depends on Lhe ouLcome of Lhe LransacLlon lf all Lhe
resource managers lnvolved ln Lhe LransacLlon voLe Lo commlL afLer phase one Lhe LransacLlon
coordlnaLor wlll send a commlL message Lo Lhe resource manager and wlll read Lhe log flle
records and make lLs updaLes permanenL lf any of Lhe parLlclpaLlng resource managers voLed
Lo aborL Lhe LransacLlon Lhe LransacLlon coordlnaLor wlll send an aborL message Lo Lhe
resource manager and wlll read lLs log flle records and undo any sysLem changes LhaL lL made
durlng phase one

Jhat | what | two phae comm|t protoco| (29C)?
A commlL operaLlon ls by deflnlLlon an allornoLhlng affalr lf a serles of operaLlons bound as a
LransacLlon cannoL be compleLed Lhe rollback musL resLore Lhe sysLem (or cooperaLlng
sysLems) Lo Lhe preLransacLlon sLaLe
ln order Lo ensure LhaL a LransacLlon can be rolled back a sofLware sysLem (daLabase sysLem)
Lyplcally logs each operaLlon lncludlng Lhe commlL operaLlon lLself A LransacLlon/recovery
manager uses Lhe log records Lo undo a parLlally compleLed LransacLlon
When a LransacLlon lnvolves mulLlple dlsLrlbuLed resources for example a daLabase server on
each of Lwo dlfferenL neLwork hosLs Lhe commlL process ls somewhaL complex because Lhe
LransacLlon lncludes operaLlons LhaL span Lwo dlsLlncL sofLware sysLems(daLabase sysLems)
each wlLh lLs own resource manager log records and so on (ln Lhls case Lhe dlsLrlbuLed
resources are Lhe daLabase servers)

Figure 8-2. Example for 2PC: A client begins a new transaction (1), executes two updates (2
& 3), and attempts to commit the changes (4). The transaction coordinator sends prepare
requests to both resource managers (5). The final step of the transaction (6) either commits
or aborts all updates. If both resource managers agree to commit the transaction, the
transaction coordinator sends a commit to all participants. If one participant wants to
abort, the transaction coordinator asks all participants to abort.
1wophase commlL ls a LransacLlon proLocol deslgned Lo solve Lhe compllcaLlons LhaL arlse wlLh
dlsLrlbuLed resource managers WlLh a Lwophase commlL proLocol Lhe dlsLrlbuLed LransacLlon
manager employs a cootJlootot(Also koowo os 1toosoctloo mooltot ) Lo manage Lhe lndlvldual
resource managers
1he commlL process proceeds as follows
O 9hase 1
4 Lach parLlclpaLlng resource manager coordlnaLes local operaLlons and forces all
log records ouL
4 lf successful respond k
4 lf unsuccessful elLher allow a LlmeouL or respond 9S
O 9hase 2
4 lf all parLlclpanLs respond k
CoordlnaLor lnsLrucLs parLlclpaLlng resource managers Lo CMMl1
9arLlclpanLs compleLe operaLlon wrlLlng Lhe log record for Lhe commlL
4 Lherwlse
CoordlnaLor lnsLrucLs parLlclpaLlng resource managers Lo 8LL8ACk
9arLlclpanLs compleLe Lhelr respecLlve local undos
ln order for Lhe scheme Lo work rellably boLh Lhe coordlnaLor and Lhe parLlclpaLlng resource
managers lndependenLly musL be able Lo guaranLee proper compleLlon lncludlng any necessary
resLarL/redo operaLlons 1he algorlLhms for guaranLeelng success by handllng fallures aL any
sLage are provlded ln advanced daLabase LexLs
MosL commonly Lhe socalled 1wo9hase CommlL 9roLocol (29C) ls used Lo ensure AClu
properLles for LransacLlons LhaL span more Lhan a slngle resource manager (db) 1ransacLlons
are coordlnaLed among dlfferenL resource managers Lhrough a LransacLlon coordlnaLor whlch
ls parL of Lhe LransacLlon monlLor AL Lhe end of each dlsLrlbuLed LransacLlon Lhe LransacLlon
coordlnaLor wlll coordlnaLe Lhe commlLmenL of Lhe LransacLlon across Lhe parLlclpaLlng
resource managers
O ln Lhe flrsL phase (prepare) all parLlclpaLlng resource managers musL ensure LhaL all
relevanL locks have been acqulred and LhaL Lhe before and afLer sLaLe of daLa LhaL
has been modlfled ln Lhe conLexL of Lhe LransacLlon has been perslsLenLly capLured
O uependlng on Lhe ouLcome of Lhe flrsL phase (voLlng) Lhe LransacLlon coordlnaLor
lnforms all parLlclpaLlng resource managers wheLher Lo commlL or rollback Lhe changes
O A slngle aborL voLe wlll cause Lhe enLlre LransacLlon Lo be rolled back helplng Lo
ensure Lhe aLomlclLy properLy of Lhe LransacLlon nly lf all parLlclpanLs voLe Lo
commlL wlll Lhey also be asked Lo make Lhe changes permanenL and vlslble
1he 29C proLocol assumes a very LlghL coupllng beLween all Lhe componenLs lnvolved ln Lhe
execuLlon of Lhe LransacLlon All parLlclpanLs musL play by Lhe rules ln order Lo ensure LhaL
Lhe ouLcome of each LransacLlon ls conslsLenL lf a parLlclpanL does noL ablde by Lhe rules Lhe
resulL wlll be a socalled heurlsLlc ouLcome LhaL ls a LransacLlon whose flnal sLaLe ls
undeflned lurLhermore deadlocks become an lssue LhaL musL be handled wlLh greaL care
1he mosL lmporLanL sLandard ln Lhe area of LransacLlon monlLors and 29C ls Lhe x/pen
sLandard for ulsLrlbuLed 1ransacLlon 9rocesslng (1he x/pen u19 sLandard)"
1be \/Opeo u1l stooJotJdeflnes Lhe socalled xA lnLerface whlch LransacLlon coordlnaLor use
Lo lnLeracL wlLh a resource manager for example Lo execuLe Lhe prepare and commlL calls
MosL commerclal 8u8MS provlde supporL for Lhe xA lnLerface enabllng Lhem Lo parLlclpaLe ln
dlsLrlbuLed LransacLlons WellesLabllshed LransacLlon coordlnaLors lnclude ClCS lMS Lnclna
(l8M) and 1uxedo (8LA) ln Lhe !ava world !1S (!ava 1ransacLlon Servlce) ls wldely esLabllshed
and lLs counLerparL ln Lhe MlcrosofL world ls M1S (MlcrosofL 1ransacLlon Server)
Jhat | 1ranact|on demarcat|on?
lor any appllcaLlon LransacLlons are coottolleJ ln Lhe buslness layer even Lhough Lhey are
Lyplcally lmplemeoteJ ln Lhe daLabase WhaL do l mean by Lhls? When you
begln/commlL/rollback a LransacLlon youre acLually lssulng commands Lo your daLabase
server lL does Lhe work for you 8uL Lhe LransacLlon Jemotcotloo ls conLrolled by Lhe buslness
loglc of Lhe appllcaLlon nly your buslness loglc knows whaL serles of SCL sLaLemenLs
comprlse a unlL of work WlLh cllenLserver apps (lncludlng web apps) Lhere ls ofLen a oneLo
one correspondence beLween requesLs and LransacLlons buL noL always

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