Chapter 18: Concurrency Control: Database System Concepts, 7 Ed
Chapter 18: Concurrency Control: Database System Concepts, 7 Ed
Database System Concepts - 7th Edition 18.2 ©Silberschatz, Korth and Sudarshan
Lock-Based Protocols (Cont.)
Lock-compatibility matrix
Database System Concepts - 7th Edition 18.3 ©Silberschatz, Korth and Sudarshan
Schedule With Lock Grants
Grants omitted in rest of
chapter
• Assume grant
happens just before
the next instruction
following lock
request
A locking protocol is a
set of rules followed by
all transactions while
requesting and releasing
locks.
Locking protocols
enforce serializability by
restricting the set of
possible schedules.
Database System Concepts - 7th Edition 18.5 ©Silberschatz, Korth and Sudarshan
Deadlock
Database System Concepts - 7th Edition 18.6 ©Silberschatz, Korth and Sudarshan
Deadlock (Cont.)
The potential for deadlock exists in most locking protocols. Deadlocks are
a necessary evil.
Starvation is also possible if concurrency control manager is badly
designed. For example:
• A transaction may be waiting for an X-lock on an item, while a
sequence of other transactions request and are granted an S-lock on
the same item.
• The same transaction is repeatedly rolled back due to deadlocks.
Concurrency control manager can be designed to prevent starvation.
Database System Concepts - 7th Edition 18.7 ©Silberschatz, Korth and Sudarshan
The Two-Phase Locking Protocol
Locks
• Transaction may not release locks
Phase 2: Shrinking Phase
• Transaction may release locks
• Transaction may not obtain locks Time
The protocol assures serializability. It can be
proved that the transactions can be
serialized in the order of their lock points
(i.e., the point where a transaction acquired
its final lock).
Database System Concepts - 7th Edition 18.8 ©Silberschatz, Korth and Sudarshan
The Two-Phase Locking Protocol (Cont.)
Database System Concepts - 7th Edition 18.9 ©Silberschatz, Korth and Sudarshan
The Two-Phase Locking Protocol (Cont.)
Database System Concepts - 7th Edition 18.10 ©Silberschatz, Korth and Sudarshan
Locking Protocols
Database System Concepts - 7th Edition 18.11 ©Silberschatz, Korth and Sudarshan
Lock Conversions
Database System Concepts - 7th Edition 18.12 ©Silberschatz, Korth and Sudarshan
Automatic Acquisition of Locks
Database System Concepts - 7th Edition 18.13 ©Silberschatz, Korth and Sudarshan
Automatic Acquisition of Locks (Cont.)
Database System Concepts - 7th Edition 18.14 ©Silberschatz, Korth and Sudarshan
Implementation of Locking
Database System Concepts - 7th Edition 18.15 ©Silberschatz, Korth and Sudarshan
Lock Table
Dark rectangles indicate granted
locks, light colored ones indicate
waiting requests
Lock table also records the type of
lock granted or requested
New request is added to the end of
the queue of requests for the data
item, and granted if it is compatible
with all earlier locks
Unlock requests result in the request
being deleted, and later requests are
checked to see if they can now be
granted
If transaction aborts, all waiting or
granted requests of the transaction
are deleted
• lock manager may keep a list of
locks held by each transaction, to
implement this efficiently
Database System Concepts - 7th Edition 18.16 ©Silberschatz, Korth and Sudarshan
Graph-Based Protocols
Database System Concepts - 7th Edition 18.17 ©Silberschatz, Korth and Sudarshan
Tree Protocol
Database System Concepts - 7th Edition 18.18 ©Silberschatz, Korth and Sudarshan
Graph-Based Protocols (Cont.)
Database System Concepts - 7th Edition 18.19 ©Silberschatz, Korth and Sudarshan
Deadlock Handling
Database System Concepts - 7th Edition 18.20 ©Silberschatz, Korth and Sudarshan
Deadlock Handling
Deadlock prevention protocols ensure that the system will never enter
into a deadlock state. Some prevention strategies:
• Require that each transaction locks all its data items before it begins
execution (pre-declaration).
• Impose partial ordering of all data items and require that a
transaction can lock data items only in the order specified by the
partial order (graph-based protocol).
Database System Concepts - 7th Edition 18.21 ©Silberschatz, Korth and Sudarshan
More Deadlock Prevention Strategies
Database System Concepts - 7th Edition 18.22 ©Silberschatz, Korth and Sudarshan
Deadlock prevention (Cont.)
Timeout-Based Schemes:
• A transaction waits for a lock only for a specified amount of time. After
that, the wait times out and the transaction is rolled back.
• Ensures that deadlocks get resolved by timeout if they occur
• Simple to implement
• But may roll back transaction unnecessarily in absence of deadlock
Difficult to determine good value of the timeout interval.
• Starvation is also possible
Database System Concepts - 7th Edition 18.23 ©Silberschatz, Korth and Sudarshan
Deadlock Detection
Wait-for graph
• Vertices: transactions
• Edge from Ti Tj. : if Ti is waiting for a lock held in conflicting mode
byTj
The system is in a deadlock state if and only if the wait-for graph has a
cycle.
Invoke a deadlock-detection algorithm periodically to look for cycles.
Database System Concepts - 7th Edition 18.24 ©Silberschatz, Korth and Sudarshan
Deadlock Recovery
Database System Concepts - 7th Edition 18.25 ©Silberschatz, Korth and Sudarshan
Multiple Granularity
Database System Concepts - 7th Edition 18.26 ©Silberschatz, Korth and Sudarshan
Example of Granularity Hierarchy
Database System Concepts - 7th Edition 18.27 ©Silberschatz, Korth and Sudarshan
Example of Granularity Hierarchy
Database System Concepts - 7th Edition 18.28 ©Silberschatz, Korth and Sudarshan
Intention Lock Modes
In addition to S and X lock modes, there are three additional lock modes
with multiple granularity:
• intention-shared (IS): indicates explicit locking at a lower level of the
tree but only with shared locks.(fine Granularity)
• intention-exclusive (IX): indicates explicit locking at a lower level with
exclusive or shared locks
• shared and intention-exclusive (SIX): the subtree rooted by that
node is locked explicitly in shared mode and explicit locking is being
done at a lower level with exclusive-mode locks.
Intention locks allow a higher level node to be locked in S or X mode
without having to check all descendent nodes.
Database System Concepts - 7th Edition 18.29 ©Silberschatz, Korth and Sudarshan
Compatibility Matrix with Intention Lock Modes
The compatibility matrix for all lock modes is:
Database System Concepts - 7th Edition 18.30 ©Silberschatz, Korth and Sudarshan
Multiple Granularity Locking Scheme
Database System Concepts - 7th Edition 18.31 ©Silberschatz, Korth and Sudarshan
Insert/Delete Operations and Predicate Reads
Database System Concepts - 7th Edition 18.32 ©Silberschatz, Korth and Sudarshan
Phantom Phenomenon
Database System Concepts - 7th Edition 18.33 ©Silberschatz, Korth and Sudarshan
Insert/Delete Operations and Predicate Reads
Database System Concepts - 7th Edition 18.34 ©Silberschatz, Korth and Sudarshan
Handling Phantoms
Database System Concepts - 7th Edition 18.35 ©Silberschatz, Korth and Sudarshan