8.concurrency Control
8.concurrency Control
Concurrent Executions
• Multiple transactions are allowed to run concurrently in the
system. Advantages are:
– increased processor and disk utilization, leading to
better transaction throughput: one transaction can be
using the CPU while another is reading from or writing to
the disk
– reduced average response time for transactions: short
transactions need not wait behind long ones.
• Concurrency control schemes – mechanisms to achieve
isolation; that is, to control the interaction among the
concurrent transactions in order to prevent them from
destroying the consistency of the database.
Concurrency control
• The concepts that will be discussed are all
based on the serializability property.
• Serializability can be maintained by
accessing the data items in exclusive
manner.
– No one can modify the data item when one
transaction access it.
– The simplest way to impalement this by using
locking concept, i.e., first lock it and then
access it.
Lock-Based Protocols
• Locking is an operation which secures
(a) permission to Read or
(b) permission to Write a data item for a
transaction.
Example: Lock (X). Data item X is locked in
behalf of the requesting transaction.
• Unlocking is an operation which removes these
permissions from the data item. Example:
Unlock (X). Data item X is made available to all
other transactions.
Lock and Unlock are Atomic operations.
Lock-Based Protocols (Cont.)
• Data items can be locked in two modes :
1. exclusive (X) mode. Data item can be both read as well as
written. X-lock is requested using lock-X instruction.
Only one write lock on X can exist at any time and no shared
lock can be applied by any other transaction on X.
• To avoid starvation:
– A request for a lock is granted if it not conflicted with the existing
lock (if any).
– A request id granted, if no other transaction is waiting for that data
item.
Locking Protocols
• Indicates when a transaction may lock or unlock an data
item.
• Restricts the number of possible schedules.
• Let { T1, T2,…, Tn} be a set of transactions in schedule S.
– We say Ti, precedes Tj,…, in S and denoted by Ti Tj,if there is a
data item Q s.t. Ti, held a lock on Q in mode A and Tj requests for a
lock, later on, in mode B where comp (A,B) =false.
– If Ti Tj, then Ti must appear before Tj.
Deadlock
T’1 T’2
read_lock (Y); T1 and T2 did follow two-phase
read_item (Y); policy but they are deadlock
read_lock (X);
read_item (X);
write_lock (X);
(waits for X) write_lock (Y);
(waits for Y)
Deadlock (T’1 and T’2)
2PLP (Cont.) [cascade rollback]
• Being a schedule serializable T1 T2 T3
it is also desirable that a
schedule should be lock-X(A)
cascadeless. Read (A)
• Cascading roll-back is Lock-S (B)
possible under two-phase
Read (B)
locking.
Write (A)
Unlock (A)
Lock-x (A)
Read (A)
Failure of T1,
Write (A)
Rollback of T1
Unlock (A)
and T2
Lock-S (A)
Read (A)
2PLP (Cont.)
Strict two-phase locking: a transaction must hold all its
exclusive locks till it commits/aborts.
transaction transaction
with smaller with larger
timestamp timestamp
IX
S
S IX
X
Multiple Granularity Locking Scheme
The set of rules which must be followed for producing
serializable schedule are
1. The lock compatibility must adhered to.
2. The root of the tree must be locked first, in any mode..
3. A node N can be locked by a transaction T in S or IX
mode only if the parent node is already locked by T in
either IS or IX mode.
4. A node N can be locked by T in X, IX, or SIX mode
only if the parent of N is already locked by T in either
IX or SIX mode.
5. T can lock a node only if it has not unlocked any node
(to enforce 2PL policy).
6. T can unlock a node, N, only if none of the children of
N are currently locked by T.
Validation-Based Protocol
• In this technique only at the time of commit serializability is
checked and transactions are aborted in case of non-
serializable schedules.
• Execution of transaction Ti is done in three phases.
1. Read and execution phase: Transaction Ti writes only to
temporary local variables
2. Validation phase: Transaction Ti performs a ``validation
test'' to determine if local variables can be written without
violating serializability.
3. Write phase: If Ti is validated, the updates are applied to
the database; otherwise, Ti is rolled back.
• The three phases of concurrently executing transactions can
be interleaved, but each transaction must go through the
three phases in that order.
Validation-Based Protocol (Cont.)
– Assume for simplicity that the validation and
write phase occur together, atomically and
serially
• I.e., only one transaction executes validation/write
at a time.
• Also called as optimistic concurrency
control since transaction executes fully in
the hope that all will go well during
validation
Validation-Based Protocol (Cont.)
Concept
Allow a transaction T’ to read a data item X while it is
write locked by a conflicting transaction T.
This is accomplished by maintaining two versions of
each data item X where one version must always have
been written by some committed transaction. This
means a write operation always creates a new version
of X.
Deadlock Handling
• Consider the following two transactions:
T1: write (X) T2: write(Y)
write(Y) write(X)
• Schedule with deadlock
T1 T2
lock-X on X
write (X)
lock-X on Y
write (Y)
wait for lock-X on X
wait for lock-X on Y
Deadlock prevention
• System is deadlocked if there is a set of transactions such that
every transaction in the set is waiting for another transaction in the
set.
• 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 (predeclaration) [conservative 2PLP]
– 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).
Deadlock avoidance
• wait-die scheme — non-preemptive
– older transaction may wait for younger one to release
data item. Younger transactions never wait for older
ones, they are rolled back instead.
– a transaction may die several times before acquiring
needed data item
• wound-wait scheme — preemptive
– older transaction wounded (I.e., roll back) the younger
transaction instead of waiting for it. Younger
transactions may wait for older ones.
– may be fewer rollbacks than wait-die scheme.
Deadlock avoidance (Cont.)
• Both in wait-die and in wound-wait schemes, a rolled back
transactions is restarted with its original timestamp. Older
transactions thus have precedence over newer ones, and starvation
is hence avoided.
• 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.
– thus deadlocks are not possible
– simple to implement; but starvation is possible. Also difficult to
determine good value of the timeout interval.
Deadlock Detection
• Deadlocks can be described as a wait-for graph, which consists
of a pair G = (V,E),
– V is a set of vertices (all the transactions in the system)
– E is a set of edges; each element is an ordered pair Ti Tj.
• If Ti Tj is in E, then there is a directed edge from Ti to Tj,
implying that Ti is waiting for Tj to release a data item.
• When Ti requests a data item currently being held by Tj, then the
edge Ti Tj is inserted in the wait-for graph. This edge is removed
only when Tj is no longer holding a data item needed by Ti.
• The system is in a deadlock state if and only if the wait-for graph
has a cycle. Must invoke a deadlock-detection algorithm
periodically to look for cycles.
Deadlock Detection (Cont.)