Mastering DBMS: Learn Basics to Advanced Technique
Deadlock in DBMS
Introduction
A deadlock occurs when two or more transactions must wait endlessly for one another in order to be terminated in a database management system (DBMS). Still, the transactions will only give up the allotted CPU and memory resources that the other one requires. Deadlock causes the entire system to halt since no job is ever completed and remains waiting indefinitely.
For instance, Transaction T1 has to update certain rows in the grade table while holding a lock on some rows in the student table. While Transaction T1 is updating the rows in the Student table that Transaction T2 locks, Transaction T2 is simultaneously holding locks on some rows in the Grade table.
The primary issue now appears. Transaction T1 is waiting for Transaction T2 to release its lock, while Transaction T2 is doing the same for Transaction T1. All activities stop and stay at a standstill condition. Until the DBMS recognises the stalemate and terminates one of the transactions, it will stay at a standstill.
The three basic techniques to control deadlocks are:
Deadlock prevention
When there is a chance of a stalemate, a transaction asking for a new lock is cancelled. When a transaction is cancelled, any modifications performed are reversed, and any locks acquired during the transaction are released. The execution of the transaction is then postponed. Because it prevents the circumstances that cause deadlocking, deadlock prevention is effective.
Deadlock detection
The database is frequently checked for deadlocks by the DBMS. A deadlock is broken by rolling back and restarting one of the transactions (the "victim") while leaving the other transaction in place.
Wait-For Graph: This deadlock detection technique builds a graph based on a transaction and the lock it has obtained (a lock is a way of preventing multiple transactions from accessing any resource simultaneously). A deadlock occurs when a cycle or loop appears in the generated graph.
Deadlock avoidance
Before it can be carried out, the transaction must acquire all required locks. This method prevents conflicting transactions from being rolled back by mandating that locks be acquired sequentially. But the serial lock assignment necessary for deadlock avoidance slows down reaction times.
Conclusion
Deadlock is an undesirable situation that causes the entire system to come to a standstill since no job is ever completed and remains waiting indefinitely. A transaction is never carried out if it might cause a deadlock with another transaction. There are four Coffman criteria, and the system may become a stalemate if any of them are met. The Wait-die and Wait-wound schemes are the two widely used strategies for preventing a deadlock, whereas deadlock management and its avoidance are techniques for dealing with the issue.