cs 4604: introducon to database management...

67
CS 4604: Introduc0on to Database Management Systems B. Aditya Prakash Lecture #17: Transac0ons 2: 2PL and Deadlocks

Upload: others

Post on 24-Dec-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

CS4604:Introduc0ontoDatabaseManagementSystems

B.AdityaPrakashLecture#17:Transac0ons2:2PLand

Deadlocks

Page 2: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Announcement

§  HW6isout– DuenextWednesday(April13)

Prakash2016 VTCS4604 2

Page 3: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Review(lastlecture)

§  DBMSssupportACIDTransac0onseman0cs.§  ConcurrencycontrolandCrashRecoveryarekeycomponents

Prakash2016 VTCS4604 3

Page 4: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Review

§  ForIsola0onproperty,serialexecu0onoftransac0onsissafebutslow– Trytofindschedulesequivalenttoserialexecu0on

§  Onesolu0onfor“conflictserializable”schedulesisTwoPhaseLocking(2PL)

Prakash2016 VTCS4604 4

Page 5: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Outline

§  2PL/2PLC§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 5

Page 6: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

SerializabilityinPrac0ce

§  DBMSdoesnottestforconflictserializabilityofagivenschedule–  Imprac0calasinterleavingofopera0onsfromconcurrentXactscouldbedictatedbytheOS

§  Approach:– Usespecificprotocolsthatareknowntoproduceconflictserializableschedules

– Butmayreduceconcurrency

Prakash2016 VTCS4604 6

Page 7: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Solu0on?

§  Onesolu0onfor“conflictserializable”schedulesisTwoPhaseLocking(2PL)

Prakash2016 VTCS4604 7

Page 8: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Answer

§  (Fullanswer:)uselocks;keepthemun0lcommit(‘strict2phaselocking’)

§  Let’sseethedetails

Prakash2016 VTCS4604 8

Page 9: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Lostupdateproblem-nolocks

T1Read(N)N=N-1Write(N) Prakash2016 VTCS4604 9

T2Read(N)N=N-1Write(N)

Page 10: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Solu0on–part1

§  withlocks:§  lockmanager:grants/denieslockrequests

Prakash2016 VTCS4604 10

Page 11: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Lostupdateproblem–withlocks

0me

T1

lock(N)

Read(N)

N=N-1

Write(N)

Unlock(N)

T2

lock(N)

lockmanager

grantslock

denieslock

T2:waits

grantslocktoT2Read(N)...

Prakash2016 VTCS4604 11

Page 12: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Locks

§  Q:Ijustneedtoread‘N’-shouldIs0llgetalock?

Prakash2016 VTCS4604 12

Page 13: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Solu0on–part1

§  Locksandtheirflavors– exclusive(orwrite-)locks– shared(orread-)locks– <andmore...>

§  compa0bilitymatrix

X

S

XST2wantsT1has

Prakash2016 VTCS4604 13

Page 14: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Solu0on–part1

§  Locksandtheirflavors– exclusive(orwrite-)locks– shared(orread-)locks– <andmore...>

§  compa0bilitymatrix

X

S

XST2wantsT1has

Yes

Prakash2016 VTCS4604 14

Page 15: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Solu0on–part1

§  transac0onsrequestlocks(orupgrades)§  lockmanagergrantsorblocksrequests§  transac0onsreleaselocks§  lockmanagerupdateslock-table

Prakash2016 VTCS4604 15

Page 16: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Solu0on–part2

locksarenotenough–eg.,the‘inconsistentanalysis’problem

Prakash2016 VTCS4604 16

Page 17: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

‘Inconsistentanalysis’

T1 Read(A) A=A-10 Write(A)

T2 Read(A) Sum = A

Read(B) Sum += B

Read(B) B=B+10 Write(B)

0me

Prakash2016 VTCS4604 17

Page 18: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

‘Inconsistentanalysis’–w/locks

0me T1

L(A)

Read(A)

...

U(A)

T2

L(A)

....

L(B)

....

theproblemremains!

T2readsaninconsistentDBstate

Solu0on??

Prakash2016 VTCS4604 18

Page 19: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Generalsolu0on:

§  Protocol(s)§ Mostpopularprotocol:2PhaseLocking(2PL)

Prakash2016 VTCS4604 19

Page 20: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

2PL

X-lockversion:transac0onsissuenolockrequests,aoerthefirst‘unlock’

THEOREM:ifALLtransac0onsinthesystemobey2PL-->allschedulesareserializable

Prakash2016 VTCS4604 20

Page 21: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

2PL–example

§  ‘inconsistentanalysis’–howdoes2PLhelp?§  howwoulditbeunder2PL?

Prakash2016 VTCS4604 21

Page 22: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

2PL–X/Slockversion

transac0onsissuenolock/upgraderequest,aoerthefirstunlock/downgrade

Ingeneral:‘growing’and‘shrinking’phase

0me

#locks

growingphase shrinkingphase

Prakash2016 VTCS4604 22

Page 23: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

2PL–X/Slockversion

transac0onsissuenolock/upgraderequest,aoerthefirstunlock/downgrade

Ingeneral:‘growing’and‘shrinking’phase

0me

#locks

viola0onof2PL

Prakash2016 VTCS4604 23

Page 24: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Two-PhaseLocking(2PL),cont.

§  2PLonitsownissufficienttoguaranteeconflictserializability(i.e.,scheduleswhoseprecedencegraphisacyclic),but,itissubjecttoCascadingAborts.

time

# locks held release phase acquisition

phase

Prakash2016 VTCS4604 24

Page 25: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

2PL

§  Problem:CascadingAborts§  Example:rollbackofT1requiresrollbackofT2!

§  Solu0on:Strict2PL,i.e,§  keepalllocks,un0l‘commit’

T1:R(A),W(A), R(B),W(B),AbortT2: R(A),W(A)

Prakash2016 VTCS4604 25

Page 26: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Strict2PL

§  Allowsonlyconflictserializableschedules,butitisactuallystrongerthanneededforthatpurpose.

# locks held

acquisition phase

time

release all locks at end of xact

Prakash2016 VTCS4604 26

Page 27: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Strict2PL==2PLC(2PL0llCommit)

§  Ineffect,“shrinkingphase”isdelayedun0l– Transac0oncommits(commitlogrecordondisk),or

– Aborts(thenlockscanbereleasedaoerrollback).

# locks held

acquisition phase

time

release all locks at end of xact

Prakash2016 VTCS4604 27

Page 28: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Prakash 2016 VT CS 4604 28

Lock_X(A) Read(A) A: = A-50 Write(A) Unlock(A)

Lock_S(A) Read(A) Unlock(A) Lock_S(B) Read(B) Unlock(B) PRINT(A+B)

Lock_X(B) Read(B) B := B +50 Write(B) Unlock(B)

Non-2PL, A= 1000, B=2000, Output =?

Page 29: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Prakash 2016 VT CS 4604 29

Lock_X(A) Read(A) A: = A-50 Write(A)

Lock_X(B) Unlock(A)

Lock_S(A) Read(A)

Read(B) B := B +50 Write(B) Unlock(B)

Lock_S(B) Unlock(A) Read(B) Unlock(B) PRINT(A+B)

2PL, A= 1000, B=2000, Output =?

Page 30: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Prakash 2016 VT CS 4604 30

Lock_X(A) Read(A) A: = A-50 Write(A)

Lock_X(B) Read(B) B := B +50 Write(B) Unlock(A) Unlock(B)

Lock_S(A) Read(A) Lock_S(B) Read(B) PRINT(A+B) Unlock(A) Unlock(B)

Strict 2PL, A= 1000, B=2000, Output =?

Page 31: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

VennDiagramforSchedulesAll Schedules

Avoid Cascading Abort

Serial

Conflict Serializable

Prakash2016 VTCS4604 31

Page 32: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Q:WhichschedulesdoesStrict2PLallow?

All Schedules

Avoid Cascading Abort

Serial

Conflict Serializable

Prakash2016 VTCS4604 32

Page 33: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Q:WhichschedulesdoesStrict2PLallow?

All Schedules

Avoid Cascading Abort

Serial

Conflict Serializable

Prakash2016 VTCS4604 33

Page 34: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

AnotherVenndiagram

2PLschedules

serializableschedules

serialsch’s2PLC

Prakash2016 VTCS4604 34

Page 35: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Outline

§  2PL/2PLC§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 35

Page 36: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

LockManagement

§  LockandunlockrequestshandledbytheLockManager(LM).

§  LMcontainsanentryforeachcurrentlyheldlock.

§  Q:structureofalocktableentry?

Prakash2016 VTCS4604 36

Page 37: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

LockManagement

§  LockandunlockrequestshandledbytheLockManager(LM).

§  LMcontainsanentryforeachcurrentlyheldlock.

§  Locktableentry:– Ptr.tolistoftransac0onscurrentlyholdingthelock

– Typeoflockheld(sharedorexclusive)– Pointertoqueueoflockrequests

Prakash2016 VTCS4604 37

Page 38: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

LockManagement,cont.

§ Whenlockrequestarrivesseeifanyotherxactholdsaconflic0nglock.–  Ifnot,createanentryandgrantthelock– Else,puttherequestoronthewaitqueue

§  Lockupgrade:transac0onthatholdsasharedlockcanbeupgradedtoholdanexclusivelock

Prakash2016 VTCS4604 38

Page 39: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

LockManagement,cont.

§  Two-phaselockingissimpleenough,right?§ We’renotdone.There’sanimportantwrinkle…

Prakash2016 VTCS4604 39

Page 40: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Prakash2016 VTCS4604 40

Example: Output = ?

Lock_X(A) Lock_S(B) Read(B) Lock_S(A)

Read(A) A: = A-50 Write(A) Lock_X(B)

Page 41: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Prakash2016 VTCS4604 41

Example: Output = ?

Lock_X(A) Lock_S(B) Read(B) Lock_S(A)

Read(A) A: = A-50 Write(A) Lock_X(B)

lock mgr: grant grant

wait

wait

Page 42: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Outline

§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 42

Page 43: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlocks

§  Deadlock:Cycleoftransac0onswai0ngforlockstobereleasedbyeachother.

§  Twowaysofdealingwithdeadlocks:– Deadlockpreven0on– Deadlockdetec0on

§ ManysystemsjustpuntanduseTimeouts– Whatarethedangerswiththisapproach?

Prakash2016 VTCS4604 43

Page 44: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

DeadlockDetec0on

§  Createawaits-forgraph:– Nodesaretransac0ons– EdgefromTitoTjifTiiswai0ngforTjtoreleasealock

§  Periodicallycheckforcyclesinwaits-forgraph

Prakash2016 VTCS4604 44

Page 45: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

DeadlockDetec0on(Con0nued)

Example: T1: S(A), S(D), S(B) T2: X(B) X(C) T3: S(D), S(C), X(A) T4: X(B)

T1 T2

T4 T3Prakash2016 VTCS4604 45

Page 46: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Anotherexample

T1 T2

T3 T4

•  is there a deadlock?

•  if yes, which xacts are involved?

Prakash2016 VTCS4604 46

Page 47: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Anotherexample

T1 T2

T3 T4

•  now, is there a deadlock?

•  if yes, which xacts are involved?

Prakash2016 VTCS4604 47

Page 48: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlockdetec0on

§  howooenshouldwerunthealgo?§  howmanytransac0onsaretypicallyinvolved?

Prakash2016 VTCS4604 48

Page 49: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlockhandling

T1 T2

T3 T4

•  Q: what to do?

Prakash2016 VTCS4604 49

Page 50: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlockhandling

T1 T2

T3 T4

•  Q0: what to do?

•  A: select a ‘victim’ & ‘rollback’

•  Q1: which/how to choose?

Prakash2016 VTCS4604 50

Page 51: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlockhandling

•  Q1: which/how to choose?

•  A1.1: by age

•  A1.2: by progress

•  A1.3: by # items locked already...

•  A1.4: by # xacts to rollback

•  Q2: How far to rollback?

T1 T2

T3 T4

Prakash2016 VTCS4604 51

Page 52: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlockhandling

•  Q2: How far to rollback?

• A2.1: completely

• A2.2: minimally

•  Q3: Starvation??

T1 T2

T3 T4

Prakash2016 VTCS4604 52

Page 53: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Deadlockhandling

•  Q3: Starvation??

•  A3.1: include #rollbacks in victim selection criterion.

T1 T2

T3 T4

Prakash2016 VTCS4604 53

Page 54: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Outline

§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 54

Page 55: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

DeadlockPreven0on

§  Assignpriori0esbasedon0mestamps(older->higherpriority)

§  Weonlyallow‘old-wait-for-young’§  (oronlyallow‘young-wait-for-old’)§  androllbackviolators.Specifically:§  SayTiwantsalockthatTjholds-twopolicies:– Wait-Die:IfTihashigherpriority,TiwaitsforTj;otherwiseTiaborts(ie.,oldwaitforyoung)

– Wound-wait:IfTihashigherpriority,Tjaborts;otherwiseTiwaits(ie.,youngwaitforold)

Prakash2016 VTCS4604 55

Page 56: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

DeadlockPreven0on

Wait-Die Wound-Wait Ti wants Tj has Ti wants Tj has

Priori0es

Prakash2016 VTCS4604 56

Page 57: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

DeadlockPreven0on

§  Q:Whydotheseschemesguaranteenodeadlocks?

§  A:§  Q:Whenatransac0onrestarts,whatisits(new)priority?

§  A:

Prakash2016 VTCS4604 57

Page 58: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

DeadlockPreven0on

§  Q:Whydotheseschemesguaranteenodeadlocks?

§  A:onlyone‘type’ofdirec0onallowed.§  Q:Whenatransac0onrestarts,whatisits(new)priority?

§  A:itsoriginal0mestamp.--Why?

Prakash2016 VTCS4604 58

Page 59: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

SQLstatement

§  usually,conc.controlistransparenttotheuser,but

§  LOCK<table-name>[EXCLUSIVE|SHARED]

Prakash2016 VTCS4604 59

Page 60: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Quiz:

§  isthereaserialschedule(=interleaving)thatisnotserializable?

§  isthereaserializableschedulethatisnotserial?

§  can2PLproduceanon-serializableschedule?(assumenodeadlocks)

Prakash2016 VTCS4604 60

Page 61: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Quiz-cont’d

§  isthereaserializableschedulethatcannotbeproducedby2PL?

§  axactobeys2PL-canitbeinvolvedinanon-serializableschedule?

§  allxactsobey2PL-cantheyendupinadeadlock?

Prakash2016 VTCS4604 61

Page 62: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Outline

§  LockManagement§  Deadlocks– detec0on– Preven0on

§  SpecializedLocking

Prakash2016 VTCS4604 62

Page 63: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Thingswewillnotstudy§  Weassumed0llnowDBobjectsarefixedandindependent---nottrueinmanycases!§  Mul0-levellocking

–  Lockdborfileorpagesorrecord?§  Whataboutlockingindexes?

–  E.g.B+-trees–  CrabbingAlgorithm

§  Whataboutdynamicdatabases?–  ‘phantom’problem–  Solu0on:predicatelocking

§  Non-lockingbasedTechniques–  TimestampbasedConcurrencyControl

§  Alltheseareinthetextbookthough

SKIP

Prakash2016 VTCS4604 63

Page 64: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Transac0onSupportinSQL-92

§  SERIALIZABLE–Nophantoms,allreadsrepeatable,no“dirty”(uncommited)reads.

§  REPEATABLEREADS–phantomsmayhappen.§  READCOMMITTED–phantomsandunrepeatablereadsmayhappen

§  READUNCOMMITTED–allofthemmayhappen.

Prakash2016 VTCS4604 64

Page 65: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Transac0onSupportinSQL-92

§  SERIALIZABLE:obtainsalllocksfirst;plusindexlocks,plusstrict2PL

§  REPEATABLEREADS–asabove,butnoindexlocks

§  READCOMMITTED–asabove,butS-locksarereleasedimmediately

§  READUNCOMMITTED–asabove,butallowing‘dirtyreads’(noS-locks)

Prakash2016 VTCS4604 65

Page 66: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Transac0onSupportinSQL-92

§  SETTRANSACTIONISOLATIONLEVELSERIALIZABLEREADONLY

§  Defaults:§  SERIALIZABLE§  READWRITE

isolation level

access mode

Prakash2016 VTCS4604 66

Page 67: CS 4604: Introducon to Database Management Systemscourses.cs.vt.edu/~cs4604/Spring16/lectures/lecture-17.pdfTwo-Phase Locking (2PL), cont. 2PL on its own is sufficient to guarantee

Conclusions

§  2PL/2PL-C(=Strict2PL):extremelypopular§  Deadlockmays0llhappen– detec0on:wait-forgraph– preven0on:abortsomexacts,defensively

§  philosophically:concurrencycontroluses:–  locks– andaborts

Prakash2016 VTCS4604 67