9c Concurrency Control 2
9c Concurrency Control 2
9c Concurrency Control 2
Lock management
• Lock manager : The part of the DBMS that keeps
track of the locks issued to transactions
• The lock manager maintains a lock table, which
is a hash table with the data object identifier as
the key.
• The DBMS also maintains a descriptive entry for
each transaction in a transaction table, and
among other things, the entry contains a pointer
to a list of locks held by the transaction.
Lock Table Entry
• A lock table entry for an object -- which can
be a page, a record, and so on, depending on
the DBMS -- contains the following
information:
1. the number of transactions currently holding a
lock on the object (this can be more than one if
the object is locked in shared mode),
2. the nature of the lock (shared or exclusive),
3. a pointer to a queue of lock requests.
Lock and Unlock Requests
• When a transaction needs a lock on an object, it issues a lock request to
the lock manager:
1. If a shared lock is requested, the queue of requests is empty, and the
object is not currently locked in exclusive mode, the lock manager
grants the lock and updates the lock table entry for the object
(indicating that the object is locked in shared mode, and incrementing
the number of transactions holding a lock by one).
2. If an exclusive lock is requested, and no transaction currently holds a
lock on the object (which also implies the queue of requests is empty),
the lock manager grants the lock and updates the lock table entry.
3. Otherwise, the requested lock cannot be immediately granted, and
the lock request is added to the queue of lock requests for this object.
The transaction requesting the lock is suspended.
• When a transaction aborts or commits, it releases all its
locks.
• When a lock on an object is released, the lock manager
updates the lock table entry for the object and examines
the lock request at the head of the queue for this object.
• If this request can now be granted, the transaction that
made the request is woken up and given the lock.
• Indeed, if there are several requests for a shared lock on
the object at the front of the queue, all of these
requests can now be granted together.
Note:
• If T1 has a shared lock on O, and T2 requests an
exclusive lock, T2's request is queued.
• Now, if T3 requests a shared lock, its request enters the
queue behind that of T2, even though the requested
lock is compatible with the lock held by T1.
• This rule ensures that T2 does not starve, that is, wait
indefinitely while a stream of other transactions acquire
shared locks and thereby prevent T2 from getting the
exclusive lock that it is waiting for.
Deadlock Prevention
• We can prevent deadlocks by giving each
transaction a priority and ensuring that lower
priority transactions are not allowed to wait for
higher priority transactions (or vice versa).
• One way to assign priorities is to give each
transaction a timestamp when it starts up.
• The lower the timestamp, the higher the
transaction's priority, that is, the oldest
transaction has the highest priority.
Schemes of Deadlock Prevention
• If a transaction Ti requests a lock and
transaction Tj holds a conflicting lock, the lock
manager can use one of the following two
policies:
1. Wait-die: If Ti has higher priority, it is allowed
to wait; otherwise it is aborted.
2. Wound-wait: If Ti has higher priority, abort
Tj; otherwise Ti waits.
• No transaction is perennially aborted because it
never has a sufficiently high priority the higher
priority transaction is never aborted.
• When a transaction is aborted and restarted, it
should be given the same timestamp that it had
originally.
• Reissuing timestamps in this way ensures that each
transaction will eventually become the oldest
transaction, and thus the one with the highest
priority, and will get all the locks that it requires.
• The wait-die scheme is nonpreemptive; only a
transaction requesting a lock can be aborted.
• As a transaction grows older (and its priority
increases), it tends to wait for more and more
younger transactions.
• A younger transaction that conflicts with an older
transaction may be repeatedly aborted (a
disadvantage with respect to wound-wait), but on
the other hand, a transaction that has all the locks it
needs will never be aborted for deadlock reasons
Deadlock Detection
• The lock manager maintains a structure called
a waits-for graph to detect deadlock cycles.
• The nodes correspond to active transactions,
and there is an arc from Ti to Tj if (and only if)
Ti is waiting for Tj to release a lock.
• The lock manager adds edges to this graph
when it queues lock requests and removes
edges when it grants lock requests
Schedule Illustrating Deadlock
Waits-for Graph before and after Deadlock
• The waits-for graph is periodically checked for
cycles, which indicate deadlock.
• A deadlock is resolved by aborting a
transaction that is on a cycle and releasing its
locks; this action allows some of the waiting
transactions to proceed.
Performance of Lock-Based Concurrency
Control
• Designing a good lock-based concurrency control
mechanism in a DBMS involves making a number of
choices:
1. Should we use deadlock-prevention or deadlock-
detection?
2. If we use deadlock-detection, how frequently should
we check for deadlocks?
3. If we use deadlock-detection and identify a deadlock,
which transaction (on some cycle in the waits-for
graph, of course) should we abort?
Blocking and Aborting
• Lock-based schemes are designed to resolve conflicts
between transactions and use one of two mechanisms:
blocking and aborting transactions.
• blocked transactions may hold locks that force other
transactions to wait
• aborting and restarting a transaction obviously wastes
the work done thus far by that transaction.
• A deadlock represents an extreme instance of blocking in
which a set of transactions is forever blocked unless one
of the deadlocked transactions is aborted by the DBMS.
deadlock-prevention or deadlock-detection?