6 ConcurrencyControl
6 ConcurrencyControl
Concurrency Control
• One of the fundamental properties of a transaction is isolation.
EmpSalary
Concurrency Control
Locks EmpSalary
There are various modes in which a data item may
be locked. We restrict our attention to two modes:
• The transaction can proceed with the operation only after the
concurrency-control manager grants the lock to the
transaction.
Concurrency Control
EmpSalary
An element comp(A, B) of the matrix has the value true if and only if mode A is
compatible with mode B.
Concurrency Control
EmpSalary
Note that shared mode is compatible with shared mode, but not with exclusive
mode.
At any time, several shared-mode locks can be held simultaneously (by different
transactions) on a particular data item.
A subsequent exclusive-mode lock request has to wait until the currently held
shared-mode locks are released.
• If Ti →Tj , then that precedence implies that in any equivalent serial schedule, Ti must
appear before Tj .
Concurrency Control
Granting of
Locks a transaction
When requests a lock on a data item in a
particular mode, and no other transaction has a lock on the
same data item in a conflicting mode, the lock can be granted.
Meanwhile, a transaction T3 may request a shared-mode lock ….T3 is granted .. it is possible that
there is a sequence of transactions that each requests a shared-mode lock on the data item ….T1
never gets the exclusive-mode lock on the data item.
Thus, a lock request will never get blocked by a lock request that is made
later.
Concurrency Control Granting of Locks –
(2)
• The Two-Phase Locking Protocol
• This protocol requires that each transaction issue lock and unlock requests in two
phases:
1. Growing phase. A transaction may obtain locks, but may not release any
lock.
Initially, a transaction
2. Shrinking is in A
phase. the growing phase.
transaction The transaction
may release acquires
locks, but may locks
not obtain
as new
any needed. Once the transaction releases a lock, it enters the shrinking phase,
locks.
and it can issue no more lock requests.
• Now, transactions can be ordered according to their lock points— this ordering is, in
fact, a serializability ordering for the transactions.
Upgrading can take place in only the growing phase, whereas downgrading can take
place in only the shrinking phase.
Concurrency Control
• Implementation of Locking
• It uses a hash table, indexed on the name of a data item, to find the
linked list (if any) for a data item; this table is called the lock table.
• Each record of the linked list for a data item notes which transaction
made the request, and what lock mode it requested.
• The record also notes if the request has currently been granted.
• This is an example of a lock table.
• The table contains locks for five different data items, I4, I7, I23, I44,
and I912.
• The lock table uses overflow chaining, so there is a linked list of data
items for each entry in the lock table.
• Example, T23 has been granted locks on I912 and I7, and is waiting
Concurrency Control • When a lock request message arrives, it adds a record to the
end of the linked list for the data item, if the linked list is
present.
• Otherwise it creates a new linked list, containing only the record
for the request.
• It always grants a lock request on a data item that is not
currently locked.
• But if the transaction requests a lock on an item on which a lock
is currently held, the lock manager grants the request only if it is
compatible with the locks that are currently held, and all earlier
requests have been granted already.
• Otherwise the request has to wait.
• Once the database system has taken appropriate actions to undo the transaction, it releases
Deadlock Handling
(Concurrency Control – Transaction Management)
Deadlock Handling
• A system is in a deadlock state if there exists a set of transactions such that
every transaction in the set is waiting for another transaction in the set.
• More precisely, there exists a set of waiting transactions {T0, T1, . . . , Tn}
such that T0 is waiting for a data item that T1 holds, and T1 is waiting for a
data item that T2 holds, and . . . , and Tn−1 is waiting for a data item that
Tn holds, and Tn is waiting for a data item that T0 holds.
(2) No cyclic waits can occur by ordering the requests for locks.
(3) Performs transaction rollback instead of waiting for a lock, whenever the wait could potentially result
in a deadlock.
The simplest scheme under the first approach requires that each
transaction locks all its data items before it begins execution.
Moreover, either all are locked in one step or none are locked.
• For example, suppose that transactions T14, T15, and T16 have
timestamps 5, 10, and 15, respectively. If T14 requests a data item
held by T15, then T14 will wait. If T16 requests a data item held by
T15, then T16 will be rolled back.
Deadlock Handling
• Two different deadlock-prevention schemes using timestamps
have been proposed: wait–die and wound–wait
• Returning to our example, with transactions T14, T15, and T16, if T14
requests a data item held by T15, then the data item will be
preempted from T15, and T15 will be rolled back. If T16 requests a
data item held by T15, then T16 will wait.
Deadlock Handling Deadlock
Prevention
• If the lock has not been granted within that time, the
transaction is said to time out, and it rolls itself back and
restarts.
Deadlock Handling
• Deadlock Detection and Recovery
• Each element in the set E of edges is an ordered pair Ti → Tj. If Ti → Tj is in E, then there is
a directed edge from transaction Ti to Tj , implying that transaction Ti is waiting for
transaction Tj to release a data item that it needs.
• A deadlock exists in the system if and only if the wait-for graph contains a
cycle.
Deadlock Handling
• The most common solution is to roll back one or more transactions to break
the deadlock.
Theory
• DBMS fundamentals
Lab
• Relational Model
• Schema Diagram • SQL
• ER Model • PL/SQL Programming
• ER Diagram • NoSQL overview
• File and Storage Structure • MongoDB – Compass
• Transaction Management installation
• Concurrency Control
• Deadlock Handling.
• Learning MongoDB
• Query Processing and Optimization • Database Connectivity
The End…