Upload
others
View
13
Download
0
Embed Size (px)
Citation preview
DatabaseManagementSystem
Lecture9
Transaction,ConcurrencyControl
*SomematerialsadaptedfromR.Ramakrishnan,J.Gehrke andShawnBowers
BasicDatabaseArchitecture
DatabaseManagementSystem 2
ConcurrencyControlandRecovery• Transactions• Awaytodefineasingle“allornothing”setofSQLactions
• Basedonasetofproperties(the“ACID”properties)
• Enableconcurrency
• Thebasisforcrashrecovery
DatabaseManagementSystem 3
Transactions• A“transaction”isasetofSQLstatements(thatmodifyadatabase)chosenbyauser
DatabaseManagementSystem 4
Transfer $100 from one account to another (MySQL): STARTTRANSACTION;SELECT@A1:=balanceFROMAccountWHEREacctno=500;
UPDATEAccountSETbalance:=@A1+100WHEREacctno=500;
SELECT@A2:=balanceFROMAccountWHEREacctno=501;
UPDATEAccountSETbalance:=@A2-100WHEREacctno=501;
COMMIT;
BEGINTRANSACTIONREADbalancefromaccount500ADD$100tobalanceofaccount500WRITEnewbalancetoaccount500READbalanceinaccount501VERIFYbalancetoseeifitcontainsatleast$100ABORTifbalanceislessthan$100SUBTRACT$100frombalanceofaccount501WRITEnewbalancetoaccount501COMMITTRANSACTION
Transactions• User(applicationdeveloper)must• Begintransaction
• Read,write,andmodifystatementsintermixedwithotherprogramminglanguagestatements(e.g.,verify)
• Pluseither• Committoindicatesuccessfulcompletionor
• Aborttoindicatethatthethetransactionshouldbe“rolledback”...i.e.,erasepreviousstepsoftransaction
• Toensuredatabasestaysinaconsistent/correctstate• TheDBMSandprogrammermustguarantee4propertiesoftransactions
• Thesearecalledthe“ACID”properties
DatabaseManagementSystem 5
ACIDProperties• Atomicity
• Consistency
• Isolation
• Durability
DatabaseManagementSystem 6
ACIDProperties
•WhatiftheOScrashedhalf-waythroughthetransaction...after$100wasremovedfromthefirstaccount?
• TherecoverymanageroftheDBMSmustassurethatthe$100isdepositedbacktothefirstaccount
• Oftencalled“rollback”
DatabaseManagementSystem 7
AtomicityA transaction happens in its entirety or not at all
ACIDProperties
• Thenotionof“consistency”isspecifictotheapplicationconstraints(definedbytheuser)
• Thustheprogrammermustensuretransactionsareconsistent• TheDBMSensuresthetransactionisatomic
• E.g.,whatifthetransactiononlydeposited$100?• Probablynotconsistentaccordingtoourexampleapplication
DatabaseManagementSystem 8
Consistency
If the DB starts in a consistent state, the transaction will transform it into a consistent state
ACIDProperties
•Whatifananothertransactioncomputedthetotalbankbalanceafter$100wasremovedfromthefirstaccount?
• Theconcurrencycontrolsubsystemmust• ensurethatalltransactionsruninisolation(i.e.,don’tmessupothertransactions)
• unlesstheprogrammerchoosesalessstrictlevelofisolation– similartoconcurrencycontrolinoperatingsystems
DatabaseManagementSystem 9
Isolation
Each transaction is “isolated” from other transactions ... DB state is as if each transaction executed by itself
ACIDProperties
•WhatifafterthecommittheOScrashedbeforethedepositwaswrittentodisk?
• Therecoverymanagermustassurethatthedepositwasatleastlogged(e.g.,tomaketheDBconsistent)
DatabaseManagementSystem 10
Durability
If a transaction commits, its changes to the DB state persist (changes are permanent)
Concurrency•Whyisconcurrencyimportant?• Betterutilizationofresources
• E.g.,whileoneuser/transactionisreadingthedisk,anothercanbeusingtheCPUorreadinganotherdisk
• Resultsinbetterthroughputandresponsetime
• Manyapplicationsrequireit(forperformance)
DatabaseManagementSystem 11
Concurrency•We’lllookatconcurrencyintermsofisolationoftransactions• Isolationisaproblemwhenmultipletransactionsarerunning,usingthesamedata,andoperationsareinterleaved
• Isolationensuredbytheconcurrencycontrolsubsystem
• Thisshouldbefamiliarifyou’vetakentheOSclass...
DatabaseManagementSystem 12
SerialSchedules• Considerthesetransactions:
• DeposittoAandwithdrawfromB
• ComputethebalanceofAandB
• ApplyinteresttoAandB
• Ascheduleisaninterleavingoftheactionsofthetransactionssothateachtransaction’sorderispreserved
• Ascheduleoftransactionsisserialifitstransactionsoccurconsecutively,oneafteranother
DatabaseManagementSystem 13
T1:BEGINA=A+100;B=B– 100;END
T2:BEGINC=A+B;END
T3:BEGINA=1.06*A;B=1.06*A;END
SerialSchedules•Whichoftheseisaschedule?Whichisserial?
DatabaseManagementSystem 14
S1T1 T3A=A+100B=B-100
A=1.06*AB=1.06*B
S2T1 T3A=A+100
B=B-100A=1.06*A
B=1.06*B
S3T1 T2A=A+100
B=B-100C=A+B
S4T1 T3B=B-100A=A+100
C=A+B
SerialSchedule!
Non-serialSchedule!
Non-serialSchedule!
NotaSchedule!
AllowableConcurrency•WhatiswrongwithS3?• Itdoesnotgivethesameresultasanyserialschedule
• TheDBMSshould• AllowserialscheduleslikeS1
• ForbidinterleavedscheduleslikeS3
• ButwhataboutS2?• NotethatitgivesthesameresultasS1!
• TheDBMSshouldalsoallowthisschedule!
• IftheDBMSonlyallowsserialschedules,thenitbecomesabatchsystem(whereistheconcurrency?)
DatabaseManagementSystem 15
SerializableSchedules• Ascheduleis“serializable”ifitseffectontheDBisthesameastheeffectonsomeserialschedule• Serialschedulesarealwaysserializable
• S2isserializable,butS3isnot
• Serializability isthesameastheisolationcondition
• Thegoaloftheconcurrencycontrolsubsystemistoensureserializability
DatabaseManagementSystem 16
SchedulesasReadsandWrites• AnexpressionA=1.06*Ameans– ReadAfromdisk• SetAequalto1.06*A
• WriteAtodisk
• Onlytheread andwrite todiskmattertotheDBMS!• We’llusethenotation...
• R(A) forReadA
• W(A) forWriteA
DatabaseManagementSystem 17
SchedulesasReadsandWrites• Theseareequal(fromDBMS/concurrencyperspective)
DatabaseManagementSystem 18
S2T1 T3A=A+100
B=B-100A=1.06*A
B=1.06*B
S2T1 T3R(A),W(A)
R(B),W(B)R(A),W(A)
R(B),W(B)
ConflictSerializability• S2hasaspecialstructurethatmakesitpossibletoshowthatitisserializable...
• Twoactionsare“nonconflicting”iftheyareindifferenttransactionsandeitherthey• Accessdifferentdatatimes(resources)
• Orbotharereads
• Ifnonconflicting actionsarecommutedthenthenewschedulegivesthesameresult
DatabaseManagementSystem 19
ConflictSerializability•woschedulesare“conflict equivalent”if• Onecanbetransformedintotheotherbycommuting(swapping)nonconflicting actions
• Ascheduleis“conflict serializable”ifitisconflictequivalenttoatleastoneserialschedule
DatabaseManagementSystem 20
Thus, every conflict serializable schedule is serializable! (... but not necessarily the other way around)
Serializability• Nonconflicting showninRed
DatabaseManagementSystem 21
S2T1 T3R(A),W(A)
R(B),W(B)R(A),W(A)
R(B),W(B)
S2T1 T3R(A),W(A)R(B),W(B)
R(A),W(A)R(B),W(B)
This is a serial schedule! This means S2 is “conflict serializable” (... and thus is serializable)
commute
Ais usedinT3,BinT1
PrecedenceGraphs• Verifying conflict serializability is tedious
• There is an easier way!
• Precedence graphs
• One node per transaction
• Edge from Ti to Tj if an action in Ti occurs before an action in Tj and the
actions conflict
• Theorem
• A schedule is conflict serializable if an only if its precedence graph is acyclic
DatabaseManagementSystem 22
T1 T2
SERIAL NOTSERIAL NOTSERIAL
S1T1 T3R(A)W(A)R(B)W(B)
R(A)W(A)R(B)W(B)
S3T1 T2R(A)W(A)
R(B)W(B)
R(A)W(A)
R(B)W(B)
S4T1 T3R(A)W(A)
R(B)W(B)
R(A)R(B)W(C)
Exercise•Withapartner,drawtheprecedencegraphsforthepreviousschedules(S1-S3)• You’llfirsthavetoconvertthemtoR’sandW’s
DatabaseManagementSystem 23
S1T1 T3A=A+100B=B-100
A=1.06*AB=1.06*B
S3T1 T2A=A+100
B=B-100C=A+B
S4T1 T3B=B-100A=A+100
C=A+B
Serializable,but..
Exercise•Withapartner,drawtheprecedencegraphsforthepreviousschedules(S1-S3)
DatabaseManagementSystem 24
T1 T2
T2
T1 T2
T2NotSerializable
S6 is actually serializable: whichever transaction writes A last “wins” Serializable but not Conflict Serializable
T1 T2 T3R(A)
R(B)
W(B)
W(A)
W(A)
R(B)
S5T1 T2 T3R(A)
W(B)W(A)
W(A)
S6
Relationships
DatabaseManagementSystem 25
Serializable
ConflictSerializable
AcyclicPrecedenceGraph
Serial
Serializability inPractice• Precedencegraphsgiveusasimplewaytoprovethatascheduleis(conflict)serializable• Buttheydonotworkforallschedules
• Intheory,thiscouldbeusedbyaDBMStocheckforserializability ...
• Inpractice• ADBMSisnotpresentedwithschedules...itonlyseesastreamoftransactions
• Instead,locking isusedtoachieve“isolation”
DatabaseManagementSystem 26
Locking• Transactionsmustobtainalockbeforereadingorupdating(writing)data
• Twokindsoflocks:• Shared(S)locks
• Exclusive(X)locks
• ToreadarecordyouMUSTgetanSlock
• Towrite(modify/delete)arecordyouMUSTgetanXlock
• Lockinformationismaintainedbya“lockmanager“
DatabaseManagementSystem 27
HowLocksWork• IfanobjecthasanS(shared)lock• newtransactionscanobtainS(shared)locks
• butnot X(exclusive)locks
• IfanobjecthasanXlock• noothertransactioncanobtainany lockonthatobject
• Ifatransactioncannotobtainalock• Itisblocked (i.e.,waitsinaqueue)
DatabaseManagementSystem 28
StrictTwoPhaseLockingProtocol(Strict2PL)• InStrict2PL• TransactionTobtains(SandX)locksgradually,asneeded
• Tholdsalllocksuntilendoftransaction(commit/abort)
DatabaseManagementSystem 29
time
#ofLocksheldbya
transactionT
01234 Alllocksarereleased
attheend,uponcommitorabort
This guarantees serializability! • Still permits interleaved schedules• But can lead to deadlock
StrictTwoPhaseLockingProtocol(Strict2PL)• Examples
DatabaseManagementSystem 30
S6T1 T2 T3R(A)
W(A)W(A)
W(A)
S6(S2PL)T1 T2 T3X(A)R(A)W(A)Commit
X(A)W(A)Commit
X(A)W(A)Commit
S7(S2PL)T1 T2
X(B)X(B)
X(C)W(C)Commit
X(A)W(A)
X(C)W(C)Commit
StrictTwoPhaseLockingProtocol(Strict2PL)• Yetanotherexample• T1:W(A),W(B)• T2:W(B),W(A)
DatabaseManagementSystem 31
S7(S2PL)T1 T2X(A)W(A)
WaitingforX(B)…
X(B)W(B)
WaitingforX(A)…
DEADLOCK!!
DeadlocksinDBMS•WhatisaDeadlock?• Acycleoftransactions,e.g.,T1...Tn whereeachTi iswaitingforTi-1toreleasealock!
• Causesthesetransactionstosleep/waitforever
• Deadlockscanoccurinstrict2PL
• Solutions• Alwaysaccessresourcesinthesameorder(impractical)
• TheDBMSwilltypicallydetect deadlocks
• ...andthenabort thetransaction(itthinks)hasusedtheleastresources
DatabaseManagementSystem 32