ADVANCE
DATABASE
SYSTEMS
DEVELOPMENT
[CC6001]
WEEK - 07
Database Concurrency II
Earlier Lecture
Concurrency can
be managed by
adding LOCKS to
the table
Deadlock - Definition
A system is in a state of
deadlock if there exists a
set of transactions such
that every transaction in
the set is waiting for
another transaction in the
set.
Deadlock - Definition
Deadlock is a situation in
which two or more transactions
are in a simultaneous wait
state, each of them waiting for
one of the others to release a
lock before it can proceed
Database
Concurrency – A
Situation Likely to
Occur
Deadlock
Deadlock
They hold locks that may be required by other
transactions
DBMS must either PREVENT or DETECT and
RESOLVE deadlock
Deadlock Problem - Example
Here, neither of transactions can proceed !
Handling Methods
Deadlock Prevention
• Prevents the deadlock state
Detection and Recovery
• Implements detection and recovery scheme
Handling Methods
May result in
Transaction Rollback
Processing Overhead
Deadlock Prevention Scheme
Each transaction locks ALL its required data items
before it begins execution.
Either ALL data items needed are locked in one step
(so the transaction can then proceed) or NONE are
locked (to avoid locking only some of the data items
needed)
Best Use Situation
This scheme could be used if the
probability of the system
entering a deadlock state is
relatively high (e.g. for long
transactions needing many locks).
Disadvantages
Low Data
Some data items could be locked for a long time before
Utilization they are used.
Possible A transaction which requires a number of data items may
find itself in a 'indefinite' wait state while at least one of the
Starvation data items is always locked by some other transactions.
Deadlock Detection & Recovery
The state of the system is examined
periodically to detect whether a deadlock
has occurred in the system.
If it has, the system attempts to recover
from the deadlock (often involving
rollback).
Deadlock Detection & Recovery Process
Maintain information about the current allocation of data items
to different transactions – namely, locks that have been granted)
Maintain information about any outstanding requests for data
items – namely, locks that have been requested but not granted
yet.
Activate an algorithm (periodically or when required) which
uses the above information to determine whether the system has
entered a deadlock state.
If a deadlock state is entered (which may involve more than one
deadlock), the system attempts to recover from the deadlock –
namely, breaking the deadlock.
Wait for Graph
A wait-for graph in computer science is a directed
graph used for deadlock detection in operating
systems and relational database systems.
In computer science, a system that allows concurrent
operation of multiple processes and locking of resources
and which does not provide mechanisms to avoid or
prevent deadlock must support a mechanism to detect
deadlocks and an algorithm for recovering from them.
Wait for Graph
WFG is a directed
graph G=(N,E) which
consists of a set of
nodes N and a set of
directed edges E.
Deadlock exists if and only if WFG contains a CYCLE
Wait for Graph - Example
Does the above graph form a CYCLE ?
Wait for Graph - Example
NO ! Hence, no DEADLOCK.
Wait for Graph
Does the above graph form a CYCLE ?
Wait for Graph
YES, T1, T4 and T2 form a CYCLE!
Hence, DEADLOCK situation.
Wait for Graph – How to Draw ?
Example Worked Out
Provide a Wait-for-
Graph to determine
whether there is a
deadlock at time t17,
and if so, how the
system could recover
from the deadlock.
Wait for Graph
Wait for Graph
Cycle formed by T1, T6
and T4, resulting in a
Deadlock
T1 waiting for T4 on D,
T4 waiting for T6 on F,
T6 waiting for T1 on B.
Deadlock Recovery - Issues
Issue of choosing a
victim
Determine which
Issue of rollback
operation
Issue of starvation
Determine how far Avoid situations
transaction(s), the chosen victim where some
among a set of transaction should transaction may
deadlocked be rolled back always be chosen
transactions, to be (total rollback or as the victim due
rolled back in partial rollback). to cost factors
order to break the based selection,
deadlock. Criteria hence never
for choosing such a completing its job.
'victim' transaction
often depends on
cost factors.
Summary
Concurrency Situations
THREE types of concurrency problems
Locking: Might result in Deadlock
Deadlock Handling
Wait for Graph for Deadlock Detection and Recovery
Thank You