Deadlock in DBMS
In this Article about Deadlock in DBMS
For example, consider the following two transactions and a schedule involving these transactions:
As is clearly seen, the schedule causes a problem. After T1 has locked A, T2 locks B and then TI tries to lock B, but unable to do so waits for T2 to unlock B. Similarly, T2 tries to lock A but finds that it is held by T1 which has not yet unlocked it and thus waits for TI to unlock A. At this stage, neither T1 nor T2 can proceed since both of these transactions are waiting for the other to unlock the locked resource.
Clearly the schedule comes to a halt in its execution. The important thing to be seen here is that both TI and T2 follow the 2PL, which guarantees serialisability. So whenever the above type of situation arises, we say that a deadlock has occurred, since two transactions are waiting for a condition that will never occur. Deadlock in DBMS
Also, the deadlock can be described in terms of a directed graph called a “wait for” graph, which is maintained by the lock manager of the DBMS. This graph G is defined by the pair (V, E). It consists of a set of vertices/nodes V is and a set of edges/arcs E. Each transaction is represented by node and an arc from Ti → Tj, if Tj holds a lock and Ti is waiting for it. When transaction Ti requests a data item currently being held by transaction Tj then the edge Ti → Tj is inserted in the “wait for” graph. This edge is removed only when transaction Tj; is no longer holding the data item needed by transaction Ti. Deadlock in DBMS
A deadlock in the system of transactions occurs, if and only if the wait-for_graph contains a cycle. Each transaction involved in the cycle is said.to be deadlocked. To detect deadlocks, a periodic check for cycles in graph can be done. For example, the “wait-for” for the schedule of transactions. TA and TB as above can be made as:
In the figure above, TA and TB are the two transactions. The two edges are present between nodes TA and TB since each is waiting for the other to unlock a resource held by the other, forming a cycle, causing a deadlock problem. The above case shows a direct cycle. However, in actual situation more than two nodes may be there in a cycle.
A deadlock is thus a situation that can be created because of locks. It causes transactions to wait forever and hence the name deadlock. A deadlock occurs because of the following conditions:
- Mutual exclusion: A resource can be locked in exclusive mode by only one transaction at a time.
- Non-preemptive locking: A data item can only be unlocked by the transaction that locked it. No other transaction can unlock it.
- Partial allocation: A transaction can acquire locks on database in a piecemeal Fashion.
- Circular waiting: Transactions lock part of data resources needed and then wait indefinitely to lock the resource currently locked by other transactions.
In order to prevent a deadlock, one has to ensure that at least one of these conditions does not occur.
A deadlock can be prevented, avoided or controlled. Let us di for deadlock prevention.
Deadlock in DBMS