Professional Documents
Culture Documents
DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan
Seewww.dbbook.comforconditionsonreuse
Chapter15:Transactions
TransactionConcept
TransactionState
ConcurrentExecutions
Serializability
Recoverability
ImplementationofIsolation
TransactionDefinitioninSQL
TestingforSerializability.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
TransactionConcept
Atransactionisaunitofprogramexecutionthataccessesand
possiblyupdatesvariousdataitems.
E.g.transactiontotransfer$50fromaccountAtoaccountB:
1. read(A)
2. A:=A50
3. write(A)
4. read(B)
5. B:=B+50
6. write(B)
Twomainissuestodealwith:
Failuresofvariouskinds,suchashardwarefailuresandsystem
crashes
Concurrentexecutionofmultipletransactions
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ExampleofFundTransfer
Transactiontotransfer$50fromaccountAtoaccountB:
1. read(A)
2. A:=A50
3. write(A)
4. read(B)
5. B:=B+50
6. write(B)
Atomicityrequirement
ifthetransactionfailsafterstep3andbeforestep6,moneywillbelost
leadingtoaninconsistentdatabasestate
Failurecouldbeduetosoftwareorhardware
thesystemshouldensurethatupdatesofapartiallyexecutedtransaction
arenotreflectedinthedatabase
Durabilityrequirementoncetheuserhasbeennotifiedthatthetransaction
hascompleted(i.e.,thetransferofthe$50hastakenplace),theupdatestothe
databasebythetransactionmustpersisteveniftherearesoftwareorhardware
failures.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ExampleofFundTransfer(Cont.)
Transactiontotransfer$50fromaccountAtoaccountB:
1.
2.
3.
4.
5.
6.
read(A)
A:=A50
write(A)
read(B)
B:=B+50
write(B)
Consistencyrequirementinaboveexample:
thesumofAandBisunchangedbytheexecutionofthetransaction
Ingeneral,consistencyrequirementsinclude
Explicitlyspecifiedintegrityconstraintssuchasprimarykeysandforeign
keys
Implicitintegrityconstraints
e.g.sumofbalancesofallaccounts,minussumofloanamounts
mustequalvalueofcashinhand
Atransactionmustseeaconsistentdatabase.
Duringtransactionexecutionthedatabasemaybetemporarilyinconsistent.
Whenthetransactioncompletessuccessfullythedatabasemustbe
consistent
Erroneoustransactionlogiccanleadtoinconsistency
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ExampleofFundTransfer(Cont.)
Isolationrequirementifbetweensteps3and6,another
transactionT2isallowedtoaccessthepartiallyupdateddatabase,it
willseeaninconsistentdatabase(thesumA+Bwillbelessthanit
shouldbe).
T1T2
1. read(A)
2. A:=A50
3. write(A)
read(A),read(B),print(A+B)
4. read(B)
5. B:=B+50
6. write(B
Isolationcanbeensuredtriviallybyrunningtransactionsserially
thatis,oneaftertheother.
However,executingmultipletransactionsconcurrentlyhassignificant
benefits,aswewillseelater.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ACIDProperties
Atransactionisaunitofprogramexecutionthataccessesandpossibly
updatesvariousdataitems.Topreservetheintegrityofdatathedatabase
systemmustensure:
Atomicity.Eitheralloperationsofthetransactionareproperlyreflected
inthedatabaseornoneare.
Consistency.Executionofatransactioninisolationpreservesthe
consistencyofthedatabase.
Isolation.Althoughmultipletransactionsmayexecuteconcurrently,
eachtransactionmustbeunawareofotherconcurrentlyexecuting
transactions.Intermediatetransactionresultsmustbehiddenfromother
concurrentlyexecutedtransactions.
Thatis,foreverypairoftransactionsTiandTj,itappearstoTithat
eitherTj,finishedexecutionbeforeTistarted,orTjstartedexecution
afterTifinished.
Durability.Afteratransactioncompletessuccessfully,thechangesit
hasmadetothedatabasepersist,eveniftherearesystemfailures.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
TransactionState
Activetheinitialstate;thetransactionstaysinthisstatewhileitis
executing
Partiallycommittedafterthefinalstatementhasbeenexecuted.
Failedafterthediscoverythatnormalexecutioncannolonger
proceed.
Abortedafterthetransactionhasbeenrolledbackandthe
databaserestoredtoitsstatepriortothestartofthetransaction.
Twooptionsafterithasbeenaborted:
restartthetransaction
canbedoneonlyifnointernallogicalerror
killthetransaction
Committedaftersuccessfulcompletion.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
TransactionState(Cont.)
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ImplementationofAtomicityand
Durability
Therecoverymanagementcomponentofadatabasesystem
implementsthesupportforatomicityanddurability.
E.g.theshadowdatabasescheme:
allupdatesaremadeonashadowcopyofthedatabase
db_pointerismadetopointtotheupdatedshadowcopyafter
thetransactionreachespartialcommitand
allupdatedpageshavebeenflushedtodisk.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ImplementationofAtomicityandDurability
(Cont.)
db_pointeralwayspointstothecurrentconsistentcopyofthedatabase.
Incasetransactionfails,oldconsistentcopypointedtobydb_pointer
canbeused,andtheshadowcopycanbedeleted.
Theshadowdatabasescheme:
Assumesthatonlyonetransactionisactiveatatime.
Assumesdisksdonotfail
Usefulfortexteditors,but
extremelyinefficientforlargedatabases(why?)
Variantcalledshadowpagingreducescopyingofdata,butis
stillnotpracticalforlargedatabases
Doesnothandleconcurrenttransactions
WillstudybetterschemesinChapter17.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConcurrentExecutions
Multipletransactionsareallowedtorunconcurrentlyinthesystem.
Advantagesare:
increasedprocessoranddiskutilization,leadingtobetter
transactionthroughput
E.g.onetransactioncanbeusingtheCPUwhileanotheris
readingfromorwritingtothedisk
reducedaverageresponsetimefortransactions:short
transactionsneednotwaitbehindlongones.
Concurrencycontrolschemesmechanismstoachieveisolation
thatis,tocontroltheinteractionamongtheconcurrent
transactionsinordertopreventthemfromdestroyingthe
consistencyofthedatabase
WillstudyinChapter16,afterstudyingnotionofcorrectness
ofconcurrentexecutions.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Schedules
Scheduleasequencesofinstructionsthatspecifythechronological
orderinwhichinstructionsofconcurrenttransactionsareexecuted
ascheduleforasetoftransactionsmustconsistofallinstructions
ofthosetransactions
mustpreservetheorderinwhichtheinstructionsappearineach
individualtransaction.
Atransactionthatsuccessfullycompletesitsexecutionwillhavea
commitinstructionsasthelaststatement
bydefaulttransactionassumedtoexecutecommitinstructionasits
laststep
Atransactionthatfailstosuccessfullycompleteitsexecutionwillhave
anabortinstructionasthelaststatement
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Schedule1
LetT1transfer$50fromAtoB,andT2transfer10%ofthe
balancefromAtoB.
AserialscheduleinwhichT1isfollowedbyT2:
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Schedule2
AserialschedulewhereT2isfollowedbyT1
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Schedule3
LetT1andT2bethetransactionsdefinedpreviously.The
followingscheduleisnotaserialschedule,butitisequivalent
toSchedule1.
InSchedules1,2and3,thesumA+Bispreserved.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Schedule4
Thefollowingconcurrentscheduledoesnotpreservethe
valueof(A+B).
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Serializability
BasicAssumptionEachtransactionpreservesdatabase
consistency.
Thusserialexecutionofasetoftransactionspreservesdatabase
consistency.
A(possiblyconcurrent)scheduleisserializableifitisequivalenttoa
serialschedule.Differentformsofscheduleequivalencegiveriseto
thenotionsof:
1. conflictserializability
2. viewserializability
Simplifiedviewoftransactions
Weignoreoperationsotherthanreadandwriteinstructions
Weassumethattransactionsmayperformarbitrarycomputations
ondatainlocalbuffersinbetweenreadsandwrites.
Oursimplifiedschedulesconsistofonlyreadandwrite
instructions.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConflictingInstructions
InstructionsliandljoftransactionsTiandTjrespectively,conflictif
andonlyifthereexistssomeitemQaccessedbybothliandlj,andat
leastoneoftheseinstructionswroteQ.
1.li=read(Q),lj=read(Q).liandljdontconflict.
2.li=read(Q),lj=write(Q).Theyconflict.
3.li=write(Q),lj=read(Q).Theyconflict
4.li=write(Q),lj=write(Q).Theyconflict
Intuitively,aconflictbetweenliandljforcesa(logical)temporalorder
betweenthem.
Ifliandljareconsecutiveinascheduleandtheydonotconflict,
theirresultswouldremainthesameeveniftheyhadbeen
interchangedintheschedule.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConflictSerializability
IfascheduleScanbetransformedintoascheduleSbyaseriesof
swapsofnonconflictinginstructions,wesaythatSandSare
conflictequivalent.
WesaythatascheduleSisconflictserializableifitisconflict
equivalenttoaserialschedule
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConflictSerializability(Cont.)
Schedule3canbetransformedintoSchedule6,aserial
schedulewhereT2followsT1,byseriesofswapsofnon
conflictinginstructions.
ThereforeSchedule3isconflictserializable.
Schedule6
Schedule3
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConflictSerializability(Cont.)
Exampleofaschedulethatisnotconflictserializable:
Weareunabletoswapinstructionsintheabovescheduletoobtain
eithertheserialschedule<T3,T4>,ortheserialschedule<T4,T3>.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ViewSerializability
LetSandSbetwoscheduleswiththesamesetoftransactions.S
andSareviewequivalentifthefollowingthreeconditionsaremet,
foreachdataitemQ,
1.
IfinscheduleS,transactionTireadstheinitialvalueofQ,thenin
scheduleSalsotransactionTimustreadtheinitialvalueofQ.
2.
IfinscheduleStransactionTiexecutesread(Q),andthatvalue
wasproducedbytransactionTj(ifany),theninscheduleSalso
transactionTimustreadthevalueofQthatwasproducedbythe
samewrite(Q)operationoftransactionTj.
3.
Thetransaction(ifany)thatperformsthefinalwrite(Q)operation
inscheduleSmustalsoperformthefinalwrite(Q)operationin
scheduleS.
Ascanbeseen,viewequivalenceisalsobasedpurelyonreadsand
writesalone.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ViewSerializability(Cont.)
AscheduleSisviewserializableifitisviewequivalenttoaserial
schedule.
Everyconflictserializablescheduleisalsoviewserializable.
Belowisaschedulewhichisviewserializablebutnotconflict
serializable.
Whatserialscheduleisaboveequivalentto?
Everyviewserializableschedulethatisnotconflictserializablehas
blindwrites.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
OtherNotionsofSerializability
Theschedulebelowproducessameoutcomeastheserial
schedule<T1,T5>,yetisnotconflictequivalentorview
equivalenttoit.
Determiningsuchequivalencerequiresanalysisofoperations
otherthanreadandwrite.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
TestingforSerializability
ConsidersomescheduleofasetoftransactionsT1,T2,...,Tn
Precedencegraphadirectgraphwheretheverticesare
thetransactions(names).
WedrawanarcfromTitoTjifthetwotransactionconflict,
andTiaccessedthedataitemonwhichtheconflictarose
earlier.
Wemaylabelthearcbytheitemthatwasaccessed.
Example1
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ExampleSchedule(ScheduleA)+PrecedenceGraph
T1
read(Y)
read(Z)
T2
read(X)
read(Y)
write(Y)
read(U)
T3
T4
read(V)
read(W)
read(W)
T1
T2
write(Z)
read(Y)
write(Y)
read(Z)
write(Z)
read(U)
write(U)
DatabaseSystemConcepts5thEdition,Sep12,2006.
T5
T4
T3
T5
15.<number>
Silberschatz,KorthandSudarshan
TestforConflictSerializability
Ascheduleisconflictserializableifandonly
ifitsprecedencegraphisacyclic.
Cycledetectionalgorithmsexistwhichtake
ordern2time,wherenisthenumberof
verticesinthegraph.
(Betteralgorithmstakeordern+e
whereeisthenumberofedges.)
Ifprecedencegraphisacyclic,the
serializabilityordercanbeobtainedbya
topologicalsortingofthegraph.
Thisisalinearorderconsistentwiththe
partialorderofthegraph.
Forexample,aserializabilityorderfor
ScheduleAwouldbe
T5T1T3T2T4
Arethereothers?
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
TestforViewSerializability
Theprecedencegraphtestforconflictserializabilitycannotbeused
directlytotestforviewserializability.
Extensiontotestforviewserializabilityhascostexponentialinthe
sizeoftheprecedencegraph.
Theproblemofcheckingifascheduleisviewserializablefallsinthe
classofNPcompleteproblems.
Thusexistenceofanefficientalgorithmisextremelyunlikely.
Howeverpracticalalgorithmsthatjustchecksomesufficient
conditionsforviewserializabilitycanstillbeused.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
RecoverableSchedules
Needtoaddresstheeffectoftransactionfailuresonconcurrently
runningtransactions.
RecoverablescheduleifatransactionTjreadsadataitem
previouslywrittenbyatransactionTi,thenthecommitoperationofTi
appearsbeforethecommitoperationofTj.
Thefollowingschedule(Schedule11)isnotrecoverableifT9commits
immediatelyaftertheread
IfT8shouldabort,T9wouldhaveread(andpossiblyshowntotheuser)
aninconsistentdatabasestate.Hence,databasemustensurethat
schedulesarerecoverable.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
CascadingRollbacks
Cascadingrollbackasingletransactionfailureleadstoa
seriesoftransactionrollbacks.Considerthefollowingschedule
wherenoneofthetransactionshasyetcommitted(sothe
scheduleisrecoverable)
IfT10fails,T11andT12mustalsoberolledback.
Canleadtotheundoingofasignificantamountofwork
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
CascadelessSchedules
Cascadelessschedulescascadingrollbackscannotoccur;for
eachpairoftransactionsTiandTjsuchthatTjreadsadataitem
previouslywrittenbyTi,thecommitoperationofTiappearsbeforethe
readoperationofTj.
Everycascadelessscheduleisalsorecoverable
Itisdesirabletorestricttheschedulestothosethatarecascadeless
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConcurrencyControl
Adatabasemustprovideamechanismthatwillensurethatallpossible
schedulesare
eitherconflictorviewserializable,and
arerecoverableandpreferablycascadeless
Apolicyinwhichonlyonetransactioncanexecuteatatimegenerates
serialschedules,butprovidesapoordegreeofconcurrency
Areserialschedulesrecoverable/cascadeless?
Testingascheduleforserializabilityafterithasexecutedisalittletoo
late!
Goaltodevelopconcurrencycontrolprotocolsthatwillassure
serializability.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ConcurrencyControlvs.SerializabilityTests
Concurrencycontrolprotocolsallowconcurrentschedules,butensure
thattheschedulesareconflict/viewserializable,andarerecoverable
andcascadeless.
Concurrencycontrolprotocolsgenerallydonotexaminethe
precedencegraphasitisbeingcreated
Insteadaprotocolimposesadisciplinethatavoidsnonseralizable
schedules.
WestudysuchprotocolsinChapter16.
Differentconcurrencycontrolprotocolsprovidedifferenttradeoffs
betweentheamountofconcurrencytheyallowandtheamountof
overheadthattheyincur.
Testsforserializabilityhelpusunderstandwhyaconcurrencycontrol
protocoliscorrect.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
WeakLevelsofConsistency
Someapplicationsarewillingtolivewithweaklevelsofconsistency,
allowingschedulesthatarenotserializable
E.g.areadonlytransactionthatwantstogetanapproximatetotal
balanceofallaccounts
E.g.databasestatisticscomputedforqueryoptimizationcanbe
approximate(why?)
Suchtransactionsneednotbeserializablewithrespecttoother
transactions
Tradeoffaccuracyforperformance
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
LevelsofConsistencyinSQL92
Serializabledefault
Repeatablereadonlycommittedrecordstoberead,repeated
readsofsamerecordmustreturnsamevalue.However,a
transactionmaynotbeserializableitmayfindsomerecords
insertedbyatransactionbutnotfindothers.
Readcommittedonlycommittedrecordscanberead,but
successivereadsofrecordmayreturndifferent(butcommitted)
values.
Readuncommittedevenuncommittedrecordsmayberead.
Lowerdegreesofconsistencyusefulforgatheringapproximate
informationaboutthedatabase
Warning:somedatabasesystemsdonotensureserializable
schedulesbydefault
E.g.OracleandPostgreSQLbydefaultsupportalevelof
consistencycalledsnapshotisolation(notpartoftheSQL
standard)
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
TransactionDefinitioninSQL
Datamanipulationlanguagemustincludeaconstructfor
specifyingthesetofactionsthatcompriseatransaction.
InSQL,atransactionbeginsimplicitly.
AtransactioninSQLendsby:
Commitworkcommitscurrenttransactionandbeginsanew
one.
Rollbackworkcausescurrenttransactiontoabort.
Inalmostalldatabasesystems,bydefault,everySQLstatement
alsocommitsimplicitlyifitexecutessuccessfully
Implicitcommitcanbeturnedoffbyadatabasedirective
E.g.inJDBC,connection.setAutoCommit(false);
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
EndofChapter
DatabaseSystemConcepts,5thEd.
Silberschatz,KorthandSudarshan
Seewww.dbbook.comforconditionsonreuse
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Schedule7
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
PrecedenceGraphfor
(a)Schedule1and(b)Schedule2
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
PrecedenceGraph
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
fig.15.21
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
ImplementationofIsolation
Schedulesmustbeconflictorviewserializable,andrecoverable,
forthesakeofdatabaseconsistency,andpreferablycascadeless.
Apolicyinwhichonlyonetransactioncanexecuteatatime
generatesserialschedules,butprovidesapoordegreeof
concurrency.
Concurrencycontrolschemestradeoffbetweentheamountof
concurrencytheyallowandtheamountofoverheadthatthey
incur.
Someschemesallowonlyconflictserializableschedulestobe
generated,whileothersallowviewserializableschedulesthatare
notconflictserializable.
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan
Figure15.6
DatabaseSystemConcepts5thEdition,Sep12,2006.
15.<number>
Silberschatz,KorthandSudarshan