Download presentation
Presentation is loading. Please wait.
Published byJadon Hahn Modified over 9 years ago
1
1 ICS 214B: Transaction Processing and Distributed Data Management Lecture 4: More on Locks Professor Chen Li
2
ICS214BNotes 042 Lock types beyond S/X locks Examples: (1) increment lock (2) update lock
3
ICS214BNotes 043 Example (1): increment lock Atomic increment action: IN i (A) {Read(A); A A+k; Write(A)} IN i (A), IN j (A) do not conflict! A=7 A=5A=17 A=15 IN i (A) +2 IN j (A) +10 IN j (A) +2 IN i (A)
4
ICS214BNotes 044 CompSXI STFF XFFF IFFT
5
ICS214BNotes 045 Update locks
6
ICS214BNotes 046 Solution If T i wants to read A and knows it may later want to write A, it requests update lock (not shared) An update lock can allow read only An update lock needs to be upgraded to allow write An update lock can be granted if there is a shared lock But once an update lock is granted, no other shared/update lock (on the same item) can be granted
7
ICS214BNotes 047 CompSXU STFT XFFF U F FF -> asymmetric table! New request Lock already held in
8
ICS214BNotes 048 Deadlock avoided
9
ICS214BNotes 049 Note: object A may be locked in different modes at the same time... S 1 =...l-S 1 (A)…l-S 2 (A)…l-U 3 (A)… l-S 4 (A)…? To grant a lock in mode t, mode t must be compatible with all currently held locks on object
10
ICS214BNotes 0410 How does locking work in practice? Every system is different (E.g., may not even provide CONFLICT-SERIALIZABLE schedules) But here is one (simplified) way...
11
ICS214BNotes 0411 (1) Don’t trust transactions to request/release locks (2) Hold all locks until transaction commits # locks time Sample Locking System:
12
ICS214BNotes 0412 Ti Read(A),Write(B) l(A),Read(A),l(B),Write(B)… Execute Scheduler, part I Scheduler, part II DB lock table Xacts don’t request locks explicitly Part I inserts necessary locks (needs to know the future) Part II executes the operations (some xacts might need to wait) Release all locks after a xact commits
13
ICS214BNotes 0413 Execute Scheduler, part I Scheduler, part II DB lock table Another example: T1: r1(A), r1(B), w1(B) T2: r2(A) r2(B) Inserted: l-s1(A) l-s2(A) l-s2(B) l-u1(B) l-x1(B) u2(B) u2(A) u1(B)u1(A) upgrade (delayed)
14
ICS214BNotes 0414 Lock table Conceptually A B C ... Lock info for B Lock info for C If null, object is unlocked Every possible object
15
ICS214BNotes 0415 But use hash table: A If object not found in hash table, it is unlocked Lock info for A A... H
16
ICS214BNotes 0416 Lock info for A - example tran mode wait? Nxt T_link Object:A Group mode:U Waiting:yes List: T1 S no T2 U no T3X yes To other T3 records
17
ICS214BNotes 0417 What are the objects we lock? ? Relation A Relation B... Tuple A Tuple B Tuple C... Disk block A Disk block B... DB
18
ICS214BNotes 0418 Locking works in any case, but should we choose small or large objects? If we lock large objects (e.g., Relations) –Need few locks –Low concurrency If we lock small objects (e.g., tuples,fields) –Need more locks –More concurrency
19
ICS214BNotes 0419 We can have it both ways!! Example Entries Rm 1Rm 2Rm 3Rm 4 ICS BLDG
20
ICS214BNotes 0420 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (S) Warning locks: “I” = “Intention to”
21
ICS214BNotes 0421 Example R1 t1t1 t2t2 t3t3 t4t4 T 1 (IS) T 1 (S), T 2 (IX) T 2 (X)
22
ICS214BNotes 0422 Multiple granularity CompRequestor IS IX S SIX X IS Holder IX S SIX X TTTTF F F F FFFFF FFFT FTFT FFTT
23
ICS214BNotes 0423 ParentChild can belocked in IS IX S SIX X P C IS, S IS, S, IX, X, SIX [S, IS] not necessary X, IX, [SIX] none
24
ICS214BNotes 0424 Rules (1) Follow multiple granularity comp function (2) Lock root of tree first, any mode (3) Node Q can be locked by Ti in S or IS only if parent(Q) locked by Ti in IX or IS (4) Node Q can be locked by Ti in X,SIX,IX only if parent(Q) locked by Ti in IX,SIX (5) Ti is two-phase (6) Ti can unlock node Q only if none of Q’s children are locked by Ti
25
ICS214BNotes 0425 Exercise: Can T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IX) T 1 (X)
26
ICS214BNotes 0426 Exercise: Can T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (X) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IX)
27
ICS214BNotes 0427 Exercise: Can T 2 access object f 3.1 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (S) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (IS)
28
ICS214BNotes 0428 Exercise: Can T 2 access object f 2.2 in S mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (SIX) T 1 (X)
29
ICS214BNotes 0429 Exercise: Can T 2 access object f 2.2 in X mode? What locks will T 2 get? R1 t1t1 t2t2 t3t3 t4t4 T 1 (IX) f 2.1 f 2.2 f 3.1 f 3.2 T 1 (SIX) T 1 (X)
30
ICS214BNotes 0430 Insert + delete operations Insert A Z ...
31
ICS214BNotes 0431 Modifications to locking rules: (1) Delete: Get exclusive lock on A before deleting A (2) Insert: At “insert A” operation by Ti, Ti is given exclusive lock on A
32
ICS214BNotes 0432 Phantoms Still have a problem: Phantoms Example: relation R (E#,name,…) constraint: E# is key use tuple locking RE#Name…. o155Smith o275Jones
33
ICS214BNotes 0433 T 1 : Insert into R T 2 : Insert into R T 1 T 2 S 1 (o 1 ) S 2 (o 1 ) S 1 (o 2 ) S 2 (o 2 ) Check Constraint Insert o 3 [99,Gore,..] Insert o 4 [99,Bush,..]...
34
ICS214BNotes 0434 Solution Use multiple granularity tree Before insert of node Q, lock parent(Q) in X mode R1 t1t1 t2t2 t3t3
35
ICS214BNotes 0435 Back to example T 1 : Insert T 2 : Insert T 1 T 2 X 1 (R) Check constraint Insert U(R) X 2 (R) Check constraint Oops! e# = 99 already in R! delayed
36
ICS214BNotes 0436 Instead of using R, can use index on R: Example: R Index 0<E#<100 Index 100<E#<200 E#=2E#=5 E#=107 E#=109...
37
ICS214BNotes 0437 Next: Tree-based concurrency control Validation concurrency control
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.