0% found this document useful (0 votes)
238 views

8.concurrency Control

Concurrency control allows multiple transactions to run concurrently in a database system. It uses locking mechanisms to control interactions between transactions to maintain consistency. The document discusses lock-based concurrency control protocols, including lock compatibility and lock operations. It describes issues like deadlocks and starvation that can arise with locking protocols and introduces the two-phase locking protocol to ensure serializable schedules.

Uploaded by

Souvik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
238 views

8.concurrency Control

Concurrency control allows multiple transactions to run concurrently in a database system. It uses locking mechanisms to control interactions between transactions to maintain consistency. The document discusses lock-based concurrency control protocols, including lock compatibility and lock operations. It describes issues like deadlocks and starvation that can arise with locking protocols and introduces the two-phase locking protocol to ensure serializable schedules.

Uploaded by

Souvik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 66

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.

2. shared (S) mode. Data item can only be read. S-lock is


requested using lock-S instruction.
More than one transaction can apply share lock on X for
reading its value but no write lock can be applied on X by
any other transaction.

• Lock requests are made to concurrency-control manager.


Transaction can proceed only after request is granted.
Lock-Based Protocols (Cont.)
• Lock-compatibility matrix

A transaction may be granted a lock on an item if the requested lock is


compatible with locks already held on the item by other transactions
• Any number of transactions can hold shared locks on an item,
– but if any transaction holds an exclusive on the item no other
transaction may hold any lock on the item.
• If a lock cannot be granted, the requesting transaction is made to
wait till all incompatible locks held by other transactions have been
released. The lock is then granted.
Database Concurrency Control

Lock Manager: Managing locks on data items.


Lock table: Lock manager uses it to store the
identify of transaction locking a data item,
the data item, lock mode and pointer to the
next data item locked. One simple way to
implement a lock table is through linked list.

Transaction ID Data item id lock mode Ptr to next data item


T1 X1 Read Next
Implementation of Locking
• A lock manager can be implemented as a separate
process to which transactions send lock and unlock
requests
• The lock manager replies to a lock request by sending a
lock grant messages (or a message asking the transaction
to roll back, in case of a deadlock)
• The requesting transaction waits until its request is
answered
• The lock manager maintains a data-structure called a lock
table to record granted locks and pending requests
• The lock table is usually implemented as an in-memory
hash table indexed on the name of the data item being
locked
Lock Table
• Black rectangles indicate granted
locks, white 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
Granted can now be granted
• If transaction aborts, all waiting or
Waiting granted requests of the transaction
are deleted
– lock manager may keep a list of
locks held by each transaction,
to implement this efficiently
Database Concurrency Control

Database requires that all transactions should


be well-formed. A transaction is well-formed if:

• It must lock the data item before it reads or


writes to it.
• It must not lock an already locked data
items and it must not try to unlock a free
data item.
Lock Operation

The following code performs the lock operation:

B: if LOCK (X) = 0 (/*item is unlocked*/)


then LOCK (X)  1 (/*lock the item*/)
else begin
wait (until lock (X) = 0) and
the lock manager wakes up the
transaction);
goto B
end;
Unlock Operation

The following code performs the unlock operation:


If(LOCK (X) ==1)
{ LOCK (X)  0 (/*unlock the item*/)
if any transactions are waiting then
wake up one of the waiting the
transactions;
}
Read Lock Operation

The following code performs the read operation:


B: if LOCK (X) = “unlocked” then
begin LOCK (X)  “read-locked”;
no_of_reads (X)  1;
end
else if LOCK (X)  “read-locked” then
no_of_reads (X)  no_of_reads (X) +1
else begin wait (until LOCK (X) = “unlocked” and
the lock manager wakes up the transaction);
go to B
end;
Write Lock Operation

The following code performs the write lock operation:


B: if LOCK (X) = “unlocked” then
begin
LOCK (X)  “write-locked”;
end
Else begin wait (until LOCK (X) = “unlocked” and
the lock manager wakes up the transaction);
go to B
end;
Unlock Operation
The following code performs the unlock operation:
if LOCK (X) = “write-locked” then
begin LOCK (X)  “unlocked”;
wakes up one of the transactions, if any
end
else if LOCK (X)  “read-locked” then
begin
no_of_reads (X)  no_of_reads (X) -1
if no_of_reads (X) = 0 then
begin
LOCK (X) = “unlocked”;
wake up one of the transactions, if any
end
end;
Lock-Based Protocols (Cont.)
• Example of a transaction performing locking:
T1: lock-X(B) T2: lock-S(A);
read (B) read (A);
B=B-50 unlock (A);
write (B) lock-S(B);
unlock (B) read (B);
lock-X (A) unlock (B);
read (A) display (A+B)
A=A+50
write (A)
unlock (A)

• Locking as above is not sufficient to guarantee serializability — if A and B get


updated in-between the read of A and B, the displayed sum would be wrong.

• A locking protocol is a set of rules followed by all transactions while


requesting and releasing locks. Locking protocols restrict the set of possible
schedules.

unlocking at the end


Pitfalls of Lock-Based Protocols
• Consider the partial schedule

• Neither T3 nor T4 can make progress — executing lock-S(B) causes


T4 to wait for T3 to release its lock on B, while executing lock-X(A)
causes T3 to wait for T4 to release its lock on A.
• Such a situation is called a deadlock.
– To handle a deadlock one of T3 or T4 must be rolled back
and its locks released.
Pitfalls of Lock-Based Protocols (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.

• 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.

• A schedule is legal under a given locking protocol, if all the


transactions of S following the rules of the protocol.
The Two-Phase Locking Protocol
• This is a protocol which ensures conflict-serializable schedules.
• Phase 1: Growing Phase
– transaction may obtain locks
– transaction may not release locks
• Phase 2: Shrinking Phase
– transaction may release locks
– transaction may not obtain locks
• 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).
• Unlock operation do not need to appear at the end of transaction.
2PLP (Cont.)
T1 T2 Result
read_lock (Y); read_lock (X); Initial values: X=20; Y=30
read_item (Y); read_item (X); Result of serial execution
unlock (Y); unlock (X); T1 followed by T2
write_lock (X); Write_lock (Y); X=50, Y=80.
read_item (X); read_item (Y); Result of serial execution
X:=X+Y; Y:=X+Y; T2 followed by T1
write_item (X); write_item (Y); X=70, Y=50
unlock (X); unlock (Y);
2PLP (Cont.)
T1 T2 Result
read_lock (Y); X=50; Y=50
read_item (Y); Nonserializable because it.
unlock (Y); violated two-phase policy.
read_lock (X);
read_item (X);
Time unlock (X);
write_lock (Y);
read_item (Y);
Y:=X+Y;
write_item (Y);
unlock (Y);
write_lock (X);
read_item (X);
X:=X+Y;
write_item (X);
unlock (X);
2PLP (Cont.)
T’1 T’2
read_lock (Y); read_lock (X); T1 and T2 follow two-phase
read_item (Y); read_item (X); policy but they are subject to
write_lock (X); Write_lock (Y); deadlock, which must be
unlock (Y); unlock (X); dealt with.
read_item (X); read_item (Y);
X:=X+Y; Y:=X+Y;
write_item (X); write_item (Y);
unlock (X); unlock (Y);
2PLP (Cont.)
Dealing with Deadlock and Starvation

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.

Rigorous two-phase locking: 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.
Lock Conversions
• Two-phase locking with lock conversions:
• First Phase:
– can acquire a lock-S on item
– can acquire a lock-X on item
– can convert a lock-S to a lock-X (upgrade)
• Second Phase:
– can release a lock-S
– can release a lock-X
– can convert a lock-X to a lock-S (downgrade)
Lock Conversions
Lock conversion
Lock upgrade: existing read lock to write lock
if Ti has a read-lock (X) and Tj has no read-lock (X) (i  j) then
convert read-lock (X) to write-lock (X)
else
force Ti to wait until Tj unlocks X

Lock downgrade: existing write lock to read lock


Ti has a write-lock (X) (*no transaction can have any lock on X*)
convert write-lock (X) to read-lock (X)
Automatic Acquisition of Locks
• A transaction Ti issues the standard read/write
instruction, without explicit locking calls.
• The operation read(D) is processed as:
if Ti has a lock on D
then
read(D)
else begin
if necessary wait until no other
transaction has a lock-X on D
grant Ti a lock-S on D;
read(D)
end
Automatic Acquisition of Locks (Cont.)
• write(D) is processed as:
if Ti has a lock-X on D
then
write(D)
else begin
if necessary wait until no other trans. has any
lock on D,
if Ti has a lock-S on D
then
upgrade lock on D to lock-X
else
grant Ti a lock-X on D
write(D)
end;
• All locks are released after commit or abort
Graph-Based Protocols
• Graph-based protocols are an alternative to two-
phase locking.
• With the prior knowledge about the order in which
the database items will be accessed.
• Impose a partial ordering  on the set D = {d1,
d2 ,..., dh} of all data items.
– If di  dj then any transaction accessing both di and dj
must access di before accessing dj.
– Implies that the set D may now be viewed as a directed
acyclic graph, called a database graph.
• The tree-protocol is a simple kind of graph protocol.
Tree Protocol

1. Only exclusive locks are allowed.


2. The first lock by Ti may be on any data item. Subsequently, a data Q
can be locked by Ti only if the parent of Q is currently locked by Ti.
3. Data items may be unlocked at any time.
4. A data item that has been locked and unlocked by Ti cannot
subsequently be relocked by Ti
Graph-Based Protocols (Cont.)
• The tree protocol ensures conflict serializability as well as freedom from
deadlock.
• Unlocking may occur earlier in the tree-locking protocol than in the two-
phase locking protocol.
– shorter waiting times, and increase in concurrency
– protocol is deadlock-free
• Drawbacks
– Protocol does not guarantee recoverability or cascade freedom
• Need to introduce commit dependencies to ensure recoverability
– Transactions may have to lock data items that they do not access.
• increased locking overhead, and additional waiting time
• potential decrease in concurrency
• Schedules not possible under two-phase locking are possible under tree
protocol, and vice versa.
Timestamp-Based Protocols
Timestamp
• A monotonically increasing variable (integer)
indicating the age of an operation or a
transaction.
• A larger timestamp value indicates a more recent
event or operation.
• Timestamp based algorithm uses timestamp to
serialize the execution of concurrent
transactions.
Timestamp-Based Protocols
• In order to assure such behavior, the protocol
maintains for each data Q two timestamp values:
– W-timestamp(Q) is the largest time-stamp of any
transaction that executed write(Q) successfully.
– R-timestamp(Q) is the largest time-stamp of any
transaction that executed read(Q) successfully
Timestamp-Based Protocols
(Cont.)
• The timestamp ordering protocol ensures that any
conflicting read and write operations are executed in
timestamp order.
• Suppose a transaction Ti issues a read(Q)
1. If TS(Ti)  W-timestamp(Q), then Ti needs to read a value of Q
that was already overwritten.
 Hence, the read operation is rejected, and Ti is rolled back.
2. If TS(Ti) W-timestamp(Q), then the read operation is executed,
and R-timestamp(Q) is set to max(R-timestamp(Q), TS(Ti)).
Timestamp-Based Protocols (Cont.)
• Suppose that transaction Ti issues write(Q).
1. If TS(Ti) < R-timestamp(Q), then the value of Q that Ti is producing was
needed previously, and the system assumed that that value would never
be produced.
 Hence, the write operation is rejected, and Ti is rolled back.
2. If TS(Ti) < W-timestamp(Q), then Ti is attempting to write an obsolete
value of Q.
 Hence, this write operation is rejected, and Ti is rolled back.
3. Otherwise, the write operation is executed, and W-timestamp(Q) is set
to TS(Ti).
Example Use of the Protocol
A partial schedule for several data items for transactions
with timestamps 1, 2, 3, 4, 5
T1 T2 T3 T4 T5
read(X)
read(Y)
read(Y)
write(Y)
write(Z)
read(Z)
read(X)
abort
read(X)
write(Z)
abort
write(Y)
write(Z)
Correctness of Timestamp-Ordering Protocol
• The timestamp-ordering protocol guarantees
serializability since all the arcs in the precedence
graph are of the form:

transaction transaction
with smaller with larger
timestamp timestamp

Thus, there will be no cycles in the precedence graph


• Timestamp protocol ensures freedom from deadlock
as no transaction ever waits.
• But the schedule may not be cascade-free, and may
not even be recoverable.
Database Concurrency Control
Timestamp based concurrency control algorithm

Strict Timestamp Ordering


1. Transaction T issues a write_item(X) operation:
a. If TS(T) > read_TS(X), then delay T until the
transaction T’ that wrote or read X has terminated
(committed or aborted).
2. Transaction T issues a read_item(X) operation:
a. If TS(T) > write_TS(X), then delay T until the
transaction T’ that wrote or read X has terminated
(committed or aborted).
Thomas’s Write Rule
1. If read_TS(X) > TS(T) then abort and roll-back T and
reject the operation.
2. If write_TS(X) > TS(T), then just ignore the write
operation and continue execution. This is because the
most recent writes counts in case of two consecutive
writes.
3. If the conditions given in 1 and 2 above do not occur,
then execute write_item(X) of T and set write_TS(X) to
TS(T).
• Thomas' Write Rule allows greater potential concurrency.
– Allows some view-serializable schedules that are not
conflict-serializable.
Multiple Granularity
A lockable unit of data defines its granularity.
Granularity can be coarse (entire database) or
it can be fine (a tuple or an attribute of a relation).
Thus, the degree of concurrency is low for coarse granularity and
high for fine granularity.
Example of data item granularity:
1. A field of a database record (an attribute of a tuple).
2. A database record (a tuple or a relation).
3. A disk block.
4. An entire file.
5. The entire database.
Example of Granularity Hierarchy

The levels, starting from the coarsest (top) level are


– database
– area
– file
– record
Intention Lock Modes
To manage such hierarchy, in addition to read and write,
three additional locking modes, called intention lock
modes are defined:
Intention-shared (IS): indicates that a shared lock(s) will be
requested on some descendent nodes(s).
Intention-exclusive (IX): indicates that an exclusive lock(s)
will be requested on some descendent nodes(s).
Shared-intention-exclusive (SIX): indicates that the current
node is locked in shared mode but an exclusive lock(s)
will be requested on some descendent nodes(s).
Compatibility Matrix with
Intention Lock Modes
• The compatibility matrix for all lock
modes is:
IS IX S S IX X
IS     

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.)

In this technique only at the time of commit serializability


is checked and transactions are aborted in case of non-
serializable schedules.
Three phases:
Read phase: A transaction can read values of committed
data items. However, updates are applied only to local
copies (versions) of the data items (in database cache).
Validation-Based Protocol (Cont.)
Validation phase: Serializability is checked before transactions
write their updates to the database.
This phase for Ti checks that, for each transaction Tj that is
either committed or is in its validation phase, one of the
following conditions holds:
1. Tj completes its write phase before Ti starts its read
phase.
2. Ti starts its write phase after Tj completes its write
phase, and the read_set of Ti has no items in common
with the write_set of Tj
3. Both the read_set and write_set of Ti have no items in
common with the write_set of Tj, and Tj completes its
read phase.
Validation-Based Protocol (Cont.)

When validating Ti, the first condition is checked first for


each transaction Tj, since (1) is the simplest condition to
check. If (1) is false then (2) is checked and if (2) is false
then (3 ) is checked. If none of these conditions holds,
the validation fails and Ti is aborted.

Write phase: On a successful validation transactions’


updates are applied to the database; otherwise,
transactions are restarted.
Validation-Based Protocol (Cont.)
• Each transaction Ti has 3 timestamps
– Start(Ti) : the time when Ti started its execution
– Validation(Ti): the time when Ti entered its validation phase
– Finish(Ti) : the time when Ti finished its write phase
• Serializability order is determined by timestamp given at validation time, to
increase concurrency.
– Thus TS(Ti) is given the value of Validation(Ti).
• This protocol is useful and gives greater degree of concurrency if probability
of conflicts is low.
– because the serializability order is not pre-decided, and
– relatively few transactions will have to be rolled back.
Validation Test for Transaction Tj
• If for all Ti with TS (Ti) < TS (Tj) either one of the following condition holds:
– finish(Ti) < start(Tj)
– start(Tj) < finish(Ti) < validation(Tj) and the set of data items written
by Ti does not intersect with the set of data items read by Tj.
then validation succeeds and Tj can be committed. Otherwise, validation
fails and Tj is aborted.
• Justification: Either the first condition is satisfied, and there is no
overlapped execution, or the second condition is satisfied and
 the writes of Tj do not affect reads of Ti since they occur after Ti has
finished its reads.
 the writes of Ti do not affect reads of Tj since Tj does not read any
item written by Ti.
Schedule Produced by Validation
• Example of schedule produced using validation
T14 T15
read(B)
read(B)
B:= B-50
read(A)
A:= A+50
read(A)
(validate)
display (A+B)
(validate)
write (B)
write (A)
Multiversion Schemes
Concept
This approach maintains a number of versions of a data
item and allocates the right version to a read operation of
a transaction. Thus unlike other mechanisms a read
operation in this mechanism is never rejected.
Side effect: Significantly more storage (RAM and disk)
is required to maintain multiple versions. To check
unlimited growth of versions, a garbage collection is run
when some criteria is satisfied.
Multiversion Schemes (Cont.)
• Multiversion schemes keep old versions of data item to
increase concurrency.
– Multiversion Timestamp Ordering
– Multiversion Two-Phase Locking
• Each successful write results in the creation of a new
version of the data item written.
• Use timestamps to label versions.
• When a read(Q) operation is issued, select an
appropriate version of Q based on the timestamp of the
transaction, and return the value of the selected version.
• reads never have to wait as an appropriate version is
returned immediately.
Multiversion Timestamp Ordering
• Each data item Q has a sequence of versions <Q1, Q2,....,
Qm>. Each version Qk contains three data fields:
– Content -- the value of version Qk.
– W-timestamp(Qk) -- timestamp of the transaction that
created (wrote) version Qk
– R-timestamp(Qk) -- largest timestamp of a transaction
that successfully read version Qk
• when a transaction Ti creates a new version Qk of Q, Qk's
W-timestamp and R-timestamp are initialized to TS(Ti).
• R-timestamp of Qk is updated whenever a transaction Tj
reads Qk, and TS(Tj) > R-timestamp(Qk).
Multiversion Timestamp Ordering (Cont)
• Suppose that transaction Ti issues a read(Q) or write(Q) operation. Let Qk
denote the version of Q whose write timestamp is the largest write timestamp
less than or equal to TS(Ti).
1. If transaction Ti issues a read(Q), then the value returned is the content of
version Qk.
2. If transaction Ti issues a write(Q)
1. if TS(Ti) < R-timestamp(Qk), then transaction Ti is rolled back.
2. if TS(Ti) = W-timestamp(Qk), the contents of Qk are overwritten
3. else a new version of Q is created.
• Observe that
– Reads always succeed
– A write by Ti is rejected if some other transaction Tj that (in the serialization
order defined by the timestamp values) should read
Ti's write, has already read a version created by a transaction older than Ti.
• Protocol guarantees serializability
Multiversion Two-Phase Locking

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.)

Wait-for graph without a cycle Wait-for graph with a cycle


Deadlock Recovery
• When deadlock is detected :
– Some transaction will have to rolled back (made a
victim) to break deadlock. Select that transaction as
victim that will incur minimum cost.
– Rollback -- determine how far to roll back transaction
• Total rollback: Abort the transaction and then restart it.
• More effective to roll back transaction only as far as
necessary to break deadlock.
– Starvation happens if same transaction is always
chosen as victim. Include the number of rollbacks in
the cost factor to avoid starvation

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy