Professional Documents
Culture Documents
22:
CHAPTER
CONCURRENCY CONTROL
TECHNIQUES
Amira Rezk
AGENDA
Introduction
Guaranteeing Serializability
by Two-Phase Locking
Result of schedule S:
X=50, Y=50
(nonserializable)
?????????????
nonserializable
GUARANTEEING SERIALIZABILITY BY
TWO-PHASE LOCKING
Two Phases:
(a) Locking (Growing / Expanding)
(b) Unlocking (Shrinking).
Locking (Growing) Phase:
A transaction applies locks (read or write) on desired
data items one at a time.
Unlocking (Shrinking) Phase:
A transaction unlocks its locked data items one at a
time.
Requirement:
For a transaction these two phases must be mutually
exclusively, that is, during locking phase unlocking
phase must not start and during unlocking phase
locking phase must not begin.
Transactions T1 and T2, which are the same as T1 and T2,
but follow the two-phase locking protocol.
TWO-PHASE LOCKING
Two-phase policy generates two locking algorithms
(a) Basic
(b) Conservative
Basic:
Transaction locks data items incrementally.
This may cause deadlock which is dealt with.
Strict:
A more stricter version of Basic algorithm
where: unlocking is performed after a
transaction terminates (commits or aborts and
rolled-back). This is the most commonly used
two-phase locking algorithm.
Conservative:
Prevents deadlock by locking all desired data
items before transaction begins execution.
DEALING WITH DEADLOCK AND
STARVATION
Deadlock occurs when each transaction T in a
set of two or more transactions is waiting for
some item that is locked by some other
transaction T in the set.
DEALING WITH DEADLOCK AND
STARVATION
Deadlock prevention
A transaction locks all data items it refers to
before it begins execution.
This way of locking prevents deadlock since a
transaction never waits for a data item.
The conservative two-phase locking uses this
approach.
DEALING WITH DEADLOCK AND
STARVATION
Deadlock detection and resolution
In this approach, deadlocks are allowed to happen.
The scheduler maintains a wait-for-graph for
detecting cycle. If a cycle exists, then one
transaction involved in the cycle is selected
(victim) and rolled-back.
A wait-for-graph is created using the lock table. As
soon as a transaction is locked, it is added to the
graph. When a chain like: Ti waits for Tj waits for
Tk waits for Ti or Tj occurs, then this creates a
cycle.
AA
A B C D
DEALING WITH DEADLOCK AND
STARVATION
Deadlock avoidance
There are many variations of two-phase locking
algorithm.
Some avoid deadlock by not letting the cycle to
complete.
That is as soon as the algorithm discovers that
blocking a transaction is likely to create a cycle, it
rolls back the transaction.
Wound-Wait and Wait-Die algorithms use
timestamps to avoid deadlocks by rolling-back
victim.
DEALING WITH DEADLOCK AND
STARVATION EXAMPLE
(SLIDE 29.)
Wait-die
If TS(Ti) < TS(Tj), then (Ti older than Tj)
Ti is allowed to wait
Otherwise (Ti younger than Tj)
Abort Ti (Ti dies) and restart it later with the same
timestamp
Wound-wait
If TS(Ti) < TS(Tj), then (Ti older than Tj)
Abort Tj (Ti wounds Tj) and restart it later with the same
timestamp
Otherwise (Ti younger than Tj)
Ti is allowed to wait
DEALING WITH DEADLOCK AND
STARVATION
Starvation
Starvation occurs when a particular transaction
consistently waits or restarted and never gets a
chance to proceed further.
In a deadlock resolution it is possible that the same
transaction may consistently be selected as
victim and rolled-back.
This limitation is inherent in all priority based
scheduling mechanisms.
One solution for starvation is to have a fair waiting
scheme, such as using a first-come-first-served
queue
In Wound-Wait scheme a younger transaction
may always be wounded (aborted) by a long
running older transaction which may create
starvation.
CONCURRENCY CONTROL BASED
ON TIMESTAMP ORDERING
(2) If TS(Ti) > TS(Tj), then Ti (younger) is forced to wait until the resource is
available.
This scheme, allows the younger transaction to wait; but when an older
transaction requests an item held by a younger one, the older transaction
forces the younger one to abort and release the item.
For example:
- If T22 requests a data item held by T23, then data item will be preempted
from T23 and T23 will be rolled back.
- If T24 requests a data item held by T23, then T24 will wait.
In both the cases, the transaction that enters the system at a later stage is
aborted