What Are Deadlocks
What Are Deadlocks
• Deadlock prevention.
• Deadlock avoidanc
• Deadlock detection and removal.
All of the three approaches can be incorporated in both a centralized and a distributed
database system.
Deadlock Prevention
The deadlock prevention approach does not allow any transaction to acquire locks
that will lead to deadlocks. The convention is that when more than one transactions
request for locking the same data item, only one of them is granted the lock.
One of the most popular deadlock prevention methods is pre-acquisition of all the
locks. In this method, a transaction acquires all the locks before starting to execute
and retains the locks for the entire duration of transaction. If another transaction
needs any of the already acquired locks, it has to wait until all the locks it needs are
available. Using this approach, the system is prevented from being deadlocked since
none of the waiting transactions are holding any lock.
Deadlock Avoidance
The deadlock avoidance approach handles deadlocks before they occur. It analyzes
the transactions and the locks to determine whether or not waiting leads to a
deadlock.
The method can be briefly stated as follows. Transactions start executing and request
data items that they need to lock. The lock manager checks whether the lock is
available. If it is available, the lock manager allocates the data item and the
transaction acquires the lock. However, if the item is locked by some other transaction
in incompatible mode, the lock manager runs an algorithm to test whether keeping
the transaction in waiting state will cause a deadlock or not. Accordingly, the algorithm
decides whether the transaction can wait or one of the transactions should be
aborted.
There are two algorithms for this purpose, namely wait-die and wound-wait. Let us
assume that there are two transactions, T1 and T2, where T1 tries to lock a data item
which is already locked by T2. The algorithms are as follows −
• Wait-Die − If T1 is older than T2, T1 is allowed to wait. Otherwise, if T1 is
younger than T2, T1 is aborted and later restarted.
• Wound-Wait − If T1 is older than T2, T2 is aborted and later restarted.
Otherwise, if T1 is younger than T2, T1 is allowed to wait.
Deadlock Detection and Removal
The deadlock detection and removal approach runs a deadlock detection algorithm
periodically and removes deadlock in case there is one. It does not check for deadlock
when a transaction places a request for a lock. When a transaction requests a lock,
the lock manager checks whether it is available. If it is available, the transaction is
allowed to lock the data item; otherwise the transaction is allowed to wait.
Since there are no precautions while granting lock requests, some of the transactions
may be deadlocked. To detect deadlocks, the lock manager periodically checks if the
wait-forgraph has cycles. If the system is deadlocked, the lock manager chooses a
victim transaction from each cycle. The victim is aborted and rolled back; and then
restarted later. Some of the methods used for victim selection are −