32
Database Management System Lecture 9 Transaction, Concurrency Control * Some materials adapted from R. Ramakrishnan, J. Gehrke and Shawn Bowers

Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

  • Upload
    others

  • View
    13

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

DatabaseManagementSystem

Lecture9

Transaction,ConcurrencyControl

*SomematerialsadaptedfromR.Ramakrishnan,J.Gehrke andShawnBowers

Page 2: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

BasicDatabaseArchitecture

DatabaseManagementSystem 2

Page 3: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

ConcurrencyControlandRecovery• Transactions• Awaytodefineasingle“allornothing”setofSQLactions

• Basedonasetofproperties(the“ACID”properties)

• Enableconcurrency

• Thebasisforcrashrecovery

DatabaseManagementSystem 3

Page 4: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 5: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 6: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

ACIDProperties• Atomicity

• Consistency

• Isolation

• Durability

DatabaseManagementSystem 6

Page 7: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

ACIDProperties

•WhatiftheOScrashedhalf-waythroughthetransaction...after$100wasremovedfromthefirstaccount?

• TherecoverymanageroftheDBMSmustassurethatthe$100isdepositedbacktothefirstaccount

• Oftencalled“rollback”

DatabaseManagementSystem 7

AtomicityA transaction happens in its entirety or not at all

Page 8: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 9: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 10: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

ACIDProperties

•WhatifafterthecommittheOScrashedbeforethedepositwaswrittentodisk?

• Therecoverymanagermustassurethatthedepositwasatleastlogged(e.g.,tomaketheDBconsistent)

DatabaseManagementSystem 10

Durability

If a transaction commits, its changes to the DB state persist (changes are permanent)

Page 11: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

Concurrency•Whyisconcurrencyimportant?• Betterutilizationofresources

• E.g.,whileoneuser/transactionisreadingthedisk,anothercanbeusingtheCPUorreadinganotherdisk

• Resultsinbetterthroughputandresponsetime

• Manyapplicationsrequireit(forperformance)

DatabaseManagementSystem 11

Page 12: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

Concurrency•We’lllookatconcurrencyintermsofisolationoftransactions• Isolationisaproblemwhenmultipletransactionsarerunning,usingthesamedata,andoperationsareinterleaved

• Isolationensuredbytheconcurrencycontrolsubsystem

• Thisshouldbefamiliarifyou’vetakentheOSclass...

DatabaseManagementSystem 12

Page 13: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 14: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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!

Page 15: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

AllowableConcurrency•WhatiswrongwithS3?• Itdoesnotgivethesameresultasanyserialschedule

• TheDBMSshould• AllowserialscheduleslikeS1

• ForbidinterleavedscheduleslikeS3

• ButwhataboutS2?• NotethatitgivesthesameresultasS1!

• TheDBMSshouldalsoallowthisschedule!

• IftheDBMSonlyallowsserialschedules,thenitbecomesabatchsystem(whereistheconcurrency?)

DatabaseManagementSystem 15

Page 16: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

SerializableSchedules• Ascheduleis“serializable”ifitseffectontheDBisthesameastheeffectonsomeserialschedule• Serialschedulesarealwaysserializable

• S2isserializable,butS3isnot

• Serializability isthesameastheisolationcondition

• Thegoaloftheconcurrencycontrolsubsystemistoensureserializability

DatabaseManagementSystem 16

Page 17: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

SchedulesasReadsandWrites• AnexpressionA=1.06*Ameans– ReadAfromdisk• SetAequalto1.06*A

• WriteAtodisk

• Onlytheread andwrite todiskmattertotheDBMS!• We’llusethenotation...

• R(A) forReadA

• W(A) forWriteA

DatabaseManagementSystem 17

Page 18: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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)

Page 19: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

ConflictSerializability• S2hasaspecialstructurethatmakesitpossibletoshowthatitisserializable...

• Twoactionsare“nonconflicting”iftheyareindifferenttransactionsandeitherthey• Accessdifferentdatatimes(resources)

• Orbotharereads

• Ifnonconflicting actionsarecommutedthenthenewschedulegivesthesameresult

DatabaseManagementSystem 19

Page 20: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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)

Page 21: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 22: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 23: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 24: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 25: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

Relationships

DatabaseManagementSystem 25

Serializable

ConflictSerializable

AcyclicPrecedenceGraph

Serial

Page 26: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

Serializability inPractice• Precedencegraphsgiveusasimplewaytoprovethatascheduleis(conflict)serializable• Buttheydonotworkforallschedules

• Intheory,thiscouldbeusedbyaDBMStocheckforserializability ...

• Inpractice• ADBMSisnotpresentedwithschedules...itonlyseesastreamoftransactions

• Instead,locking isusedtoachieve“isolation”

DatabaseManagementSystem 26

Page 27: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

Locking• Transactionsmustobtainalockbeforereadingorupdating(writing)data

• Twokindsoflocks:• Shared(S)locks

• Exclusive(X)locks

• ToreadarecordyouMUSTgetanSlock

• Towrite(modify/delete)arecordyouMUSTgetanXlock

• Lockinformationismaintainedbya“lockmanager“

DatabaseManagementSystem 27

Page 28: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

HowLocksWork• IfanobjecthasanS(shared)lock• newtransactionscanobtainS(shared)locks

• butnot X(exclusive)locks

• IfanobjecthasanXlock• noothertransactioncanobtainany lockonthatobject

• Ifatransactioncannotobtainalock• Itisblocked (i.e.,waitsinaqueue)

DatabaseManagementSystem 28

Page 29: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 30: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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

Page 31: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

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!!

Page 32: Database Management System - gnu.ac.kropen.gnu.ac.kr/lecslides/2017-2-DB/DBLec09_Concurrency.pdf · Database Management System Lecture 9 Transaction, Concurrency Control * Some materials

DeadlocksinDBMS•WhatisaDeadlock?• Acycleoftransactions,e.g.,T1...Tn whereeachTi iswaitingforTi-1toreleasealock!

• Causesthesetransactionstosleep/waitforever

• Deadlockscanoccurinstrict2PL

• Solutions• Alwaysaccessresourcesinthesameorder(impractical)

• TheDBMSwilltypicallydetect deadlocks

• ...andthenabort thetransaction(itthinks)hasusedtheleastresources

DatabaseManagementSystem 32