Whenever a deadlock occurs in DBMS it must be handled with care. DBMS Deadlock Handling involves preventing or avoiding deadlocks . The following approaches may be followed.
Deadlock Prevention Approaches are usually used in large databases. To make sure that the schedule never enters a deadlock state there are various approaches used in DBMS.
One approach for DBMS Deadlock Handling is to lock all the data items used by the transaction before the start of the execution of that transaction. If anyone of the data items is not being obtained then none of the data items should be locked for that transaction. Hence the transaction waits for the data items to be unlocked by other transactions and tries for the lock again.
This approach ensures that partial allocation of resources is not done to the transactions. Hence a deadlock may be prevented.
But there are two main disadvantages of this approach. These are:
- It is usually difficult to determine all the data items which are required by the transaction beforehand.
- If all data items are locked in advance utilisation of data items may be low. as the data items may be locked much before they come to use.
In the second approach for deadlock prevention, an arbitrary serial order is assigned to the data items. This approach ensures thatdata items are locked according to this arbitrary serial order. Hence this approach helps in avoiding the circular wait condition.
But this approach also has a disadvantage. this approach reduces consistency considerably.
In the third approach for deadlock prevention timestamp values are used with transactions.
A timestamp is basically a unique identifier assigned to each transaction which is associatedwith when the transaction starts its execution.
For instance, let us assume two transactions T1 and T2. If transaction T1 starts executing before transaction T2 then the timestamp of T1 will be less than the timestamp of T2.
In this approach these time stamps help in deciding which transactions need to wait and which transactions have to abort and roll back.
Deadlock Prevention Schemes
Another way of DBMS deadlock handling deadlocks is to use deadlock prevention protocols. These protocols are:
- Two face Locking – in this each transaction locks all the items it needs in advance.
- No wait Algorithm – in this effort transaction is unable to obtain a lock it immediately aborts and then restarts after some time.
- Using the concept of Timestamp Ordering -According to the time stamps there are two methods of deadlock prevention.
- Wait DieProtocol: This protocol allows older transactions to wait for younger ones but rollbacks are younger transaction that wants a lock held by an older one.
- Wound WaitProtocol: this protocol allows younger transactions to wait for older ones but if an older transaction requests a lock that a young girl one holds the younger one is rolled back.
Deadlock Detection and Recovery
In order to detect a deadlock, Deadlock Detection Techniquesare used these are as follows:
- Wait for Graph
- Time out Method
In this technique deadlocks are detected by constructing a directed graph known as a wait for graph.
The nodes of the graph depict activetransactions and the edges show the relation between the transactions waiting for the release of locks heldby other transactions.
A cycle in the wait for graph shows that there is a deadlock in transactions. This deadlock can only be broken by aborting a transaction. The transaction thus chosen is known as a victim.
Time out Method
In this method the system defines a time out period for each transaction. The system then compares the transactions wait periodwith that of the system defined time out period.
If the transactions wait period is more than that of the time out period then the system assumes that the transaction may be deadlocked and the system aborts it.
Deadlocks can be avoided in advance if information about processes is available before resources are allocated to them.The system keeps a record of all the resources requested by processes so that is safe state of execution could be maintained. Based on the information resources are granted to various processes.
Banker’s algorithm is one of the well-known algorithms used for deadlock avoidance. But for many systems it is not possible to know in advance about the resources which process will request during its execution. As a result most of the time deadlock avoidance is not possible.