Download presentation
Presentation is loading. Please wait.
1
Database Management System
Lecture - 45 © Virtual University of Pakistan
2
© Virtual University of Pakistan
Locking Idea Before performing an operation a transaction applies for a S/E lock on an object If object free or lock compatible then lock granted otherwise transaction will wait © Virtual University of Pakistan
3
© Virtual University of Pakistan
Deadlock Deadlock is a situation when two transactions are waiting for each other to release a lock The transaction involved in deadlock keep on waiting unless deadlock is over © Virtual University of Pakistan
4
© Virtual University of Pakistan
Deadlock TIME TA TB t1 XLock (X) ……… t2 ……. XLock (Y) t3 Request Y …….. t4 Wait Request X t5 t6 © Virtual University of Pakistan
5
© Virtual University of Pakistan
Deadlock Handling Deadlock prevention Deadlock detection and resolution Prevention is not always possible Deadlock is detected by wait-for graph © Virtual University of Pakistan
6
© Virtual University of Pakistan
Wait-for Graph TA TB TN TA TB TC TA TD Victim Transaction © Virtual University of Pakistan
7
© Virtual University of Pakistan
Two-Phase Locking 2PL ensures serializability but generates deadlock The locks are granted and released in two phases, the growing and shrinking phase © Virtual University of Pakistan
8
© Virtual University of Pakistan
Two-Phase Locking A transaction must acquire a lock before doing any operation Locks are only granted in growing phase, once a transaction enters in shrinking phase no locks can acquire © Virtual University of Pakistan
9
© Virtual University of Pakistan
Levels of Locking Lock can be applied on attribute, record, file, group of files or even entire database Granularity of locks Finer the granularity more concurrency but more overhead © Virtual University of Pakistan
10
© Virtual University of Pakistan
Granularity of Locks Database Area A Area B Area C File A1 File A2 File A3 Rec A11 Rec A12 Rec A13 At A111 At A112 At A113 ……. © Virtual University of Pakistan
11
© Virtual University of Pakistan
Granularity of Locks When a lock at lower level is applied, compatibility is checked upward How will a lock at higher level know about a lock at lower level Individual checking is not feasible © Virtual University of Pakistan
12
© Virtual University of Pakistan
Intension Locks When a lock is applied on a node, an intension shared (IS) or intension exclusive (IE) lock is applied on all nodes till the root An intension lock at a node indicates a lock at the lower level © Virtual University of Pakistan
13
© Virtual University of Pakistan
Intension Locks While applying for a lock on a node, compatibility with the intension lock is also checked Share intension lock (SIX) Conflicts with locks conflicting with S or IX; only one SIX © Virtual University of Pakistan
14
© Virtual University of Pakistan
Extended Comp Matrix S IS X IX SIX N Y IS S IX X SIX Y N © Virtual University of Pakistan
15
© Virtual University of Pakistan
Locking Summary © Virtual University of Pakistan
16
© Virtual University of Pakistan
Timestamping No locks are used Guarantees consistency and no deadlocks, however, certain transactions are cancelled Based on the concept of timestamp © Virtual University of Pakistan
17
© Virtual University of Pakistan
Timestamp An identifier that determines the relative order of the transactions Greater timestamp younger the transaction The transactions’ operations are allowed in the timestamp order © Virtual University of Pakistan
18
© Virtual University of Pakistan
Timestamp Timestamps are also allocated to data items as well A read timestamp and a write timestamp to determine the last transaction that read or wrote © Virtual University of Pakistan
19
© Virtual University of Pakistan
Two Problems When a transaction wants to read an item that has been updated by a younger transaction A transaction wants to write an item that has been read or written by a younger transaction © Virtual University of Pakistan
20
© Virtual University of Pakistan
Versions of Data Items First problem is met by maintaining multiple versions of data items Value read-timestamp write-timestamp 742 6 8 © Virtual University of Pakistan
21
© Virtual University of Pakistan
Versions of Data Items Value read-timestamp write-timestamp 742 6 1096 9 V1 V2 Transaction 8 wants to read the data item © Virtual University of Pakistan
22
© Virtual University of Pakistan
Versions of Data Items Value read-timestamp write-timestamp V1 742 6 8 6 V2 1096 9 9 Transaction 8 gets the smaller nearest value Read timestamp is changed © Virtual University of Pakistan
23
© Virtual University of Pakistan
Problem 2 Write request when an item read or written by smaller transaction Value read-timestamp write-timestamp 742 8 6 1096 9 V1 V2 Transaction 7 wants to write the item © Virtual University of Pakistan
24
© Virtual University of Pakistan
The End Thanks all © Virtual University of Pakistan
25
© Virtual University of Pakistan
Timestamp Algorithm On read request Check the read TS of latest version If latest write TS < requested TS Give the value of latest otherwise find the largest smaller write TS and return that value Update the read TS of data item © Virtual University of Pakistan
26
© Virtual University of Pakistan
Timestamp Algorithm On write request Check the read ant write TS of latest version If either one is greater than the requested TS, cancel the transaction and restart If none of latest is greater Create a new version, place new value and current TS in write TS of data item © Virtual University of Pakistan
27
Database Management System
Lecture - 45 © Virtual University of Pakistan
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.