Deadlock in DBMS
In Database Management Systems (DBMS), a deadlock is a situation in which two or more transactions wait indefinitely for resources that are held by each other. Since none of the transactions can continue, the system may experience delays and reduced performance.
Deadlocks commonly occur when multiple transactions access and lock the same database resources in different orders. A DBMS must detect, prevent, or recover from deadlocks to maintain reliable transaction processing.
Table of Contents

Understanding Deadlock with an Example
Consider two transactions, T1 and T2, working with the Student and Grade tables.
- Transaction T1 locks some rows in the
Studenttable and then requests a lock on rows in theGradetable. - Transaction T2 already holds a lock on rows in the
Gradetable and requests a lock on rows in theStudenttable.
The situation becomes:
- T1 waits for T2 to release the lock on
Grade. - T2 waits for T1 to release the lock on
Student.
Neither transaction can proceed because each is waiting for a resource held by the other. This creates a deadlock.
To resolve the situation, the DBMS can detect the deadlock and roll back one of the transactions, allowing the other transaction to continue.
Simple Deadlock Example
| Transaction | First Lock | Second Request | Status |
|---|---|---|---|
| T1 | Student | Grade | Waiting for T2 |
| T2 | Grade | Student | Waiting for T1 |
This creates a circular dependency:
T1 → T2 → T1
Because the transactions are waiting for each other, the DBMS must take action to break the cycle.
Conditions for Deadlock
A deadlock can occur only when all four of the following conditions exist simultaneously:
Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
1. Mutual Exclusion
At least one resource must be held in a way that allows only one transaction to use it at a time. Other transactions requesting that resource must wait.
2. Hold and Wait
A transaction holds one or more resources while waiting to acquire additional resources held by other transactions.
3. No Preemption
A resource cannot be forcibly taken away from a transaction. It must be released voluntarily after the transaction completes or rolls back.
4. Circular Wait
A circular chain of transactions exists where each transaction is waiting for a resource held by the next transaction in the chain.
For example:
T1 → T2 → T3 → T1
If at least one of these four conditions is prevented, a deadlock can be avoided.
Deadlock Handling in DBMS
DBMSs generally use three major approaches to handle deadlocks:
- Deadlock Prevention
- Deadlock Avoidance
- Deadlock Detection and Recovery
Deadlock Avoidance
Deadlock avoidance attempts to ensure that the system does not enter an unsafe state that could result in a deadlock.
The DBMS analyzes resource requirements and transaction behavior before granting resources. The objective is to allocate resources only when doing so keeps the system in a safe state.
In practical database systems, different locking and transaction-management techniques may be used depending on the DBMS architecture and workload.
Wait-for Graph
A Wait-for Graph is a directed graph used to represent which transactions are waiting for resources held by other transactions.
- Each node represents a transaction.
- An edge from T1 to T2 means that T1 is waiting for a resource held by T2.
- A cycle in the graph indicates a deadlock.
For example:
T1 → T2 → T3 → T1
The cycle shows that the transactions are waiting for one another, indicating a deadlock.
Deadlock Prevention
Deadlock prevention focuses on ensuring that at least one of the four necessary deadlock conditions never occurs.
One common approach is to use timestamp-based schemes. Each transaction receives a timestamp when it starts. The DBMS then uses transaction age to determine which transaction should wait and which should be aborted.
Timestamp-Based Schemes
1. Wait-Die Scheme
The Wait-Die scheme is a non-preemptive timestamp-based technique.
- An older transaction is allowed to wait for a younger transaction.
- A younger transaction requesting a resource held by an older transaction is aborted and restarted.
For example, if T1 is older than T2 and T1 requests a resource held by T2, T1 can wait. If T2 requests a resource held by T1, T2 is aborted and restarted.
2. Wound-Wait Scheme
The Wound-Wait scheme is a preemptive timestamp-based technique.
- An older transaction can force a younger transaction holding a required resource to abort.
- A younger transaction requesting a resource held by an older transaction waits.
This approach can reduce unnecessary waiting and may result in fewer transaction restarts in some workloads.
Wait-Die vs Wound-Wait
| Feature | Wait-Die | Wound-Wait |
|---|---|---|
| Technique | Non-preemptive | Preemptive |
| Older transaction requesting younger’s resource | Waits | Forces younger transaction to abort |
| Younger transaction requesting older’s resource | Aborts | Waits |
| Resource preemption | No | Yes |
| Main idea | Older transactions wait | Older transactions get priority |
Deadlock Detection and Recovery
In some database systems, deadlocks are allowed to occur and are handled after they are detected. The DBMS periodically checks the transaction state and searches for cycles in the Wait-for Graph.
If a cycle is detected, the system identifies the transactions involved and performs recovery to break the deadlock.
Recovery from Deadlock
Once a deadlock has been detected, the DBMS generally performs the following steps:
1. Select a Victim Transaction
The DBMS selects one transaction involved in the deadlock to roll back. The choice may depend on factors such as how much work the transaction has completed, how many resources it holds, and the cost of restarting it.
2. Rollback the Transaction
The selected transaction is rolled back, either completely or to an appropriate earlier point, depending on the recovery mechanism being used.
Rolling back the transaction releases its locks and resources, allowing the remaining transaction or transactions to continue.
3. Avoid Starvation
The DBMS should ensure that the same transaction is not repeatedly selected as the victim. Otherwise, a transaction could experience starvation, where it is continuously delayed or restarted.
Difference Between Deadlock Prevention, Avoidance, and Detection
| Approach | Purpose | Basic Idea |
|---|---|---|
| Prevention | Stop deadlocks from occurring | Prevent at least one necessary deadlock condition |
| Avoidance | Avoid unsafe resource allocation | Grant resources only when the resulting state remains safe |
| Detection and Recovery | Handle deadlocks after they occur | Detect cycles and roll back selected transactions |
Advantages of Deadlock Handling
- Improves database system reliability.
- Prevents transactions from waiting indefinitely.
- Helps maintain efficient resource utilization.
- Reduces the impact of transaction conflicts.
- Improves overall database performance.
YT:- DecodeIT
How to Reduce Deadlocks in DBMS
Although deadlocks cannot always be completely eliminated in systems with concurrent transactions, their occurrence can often be reduced through good database and transaction design.
- Keep transactions as short as possible.
- Access database resources in a consistent order.
- Avoid holding locks longer than necessary.
- Use appropriate isolation and locking strategies.
- Detect and recover from deadlocks efficiently.
- Use suitable transaction retry mechanisms after rollback.
Conclusion
A deadlock in DBMS occurs when two or more transactions wait indefinitely for resources held by one another. The four necessary conditions are mutual exclusion, hold and wait, no preemption, and circular wait.
DBMSs handle deadlocks through prevention, avoidance, or detection and recovery. Techniques such as Wait-Die, Wound-Wait, and Wait-for Graphs help database systems manage transaction conflicts and maintain reliable performance.
Understanding deadlocks is essential for designing efficient database applications, especially when many transactions are executing concurrently.
Keywords
Deadlock in DBMS, DBMS deadlock, deadlock prevention, deadlock avoidance, deadlock detection, deadlock recovery, wait-for graph, Wait-Die scheme, Wound-Wait scheme, timestamp based protocol, concurrency control in DBMS, transaction management, database deadlock, deadlock conditions Deadlock Deadlock in DBMS, DBMS Deadlock, Deadlock Prevention, Deadlock Avoidance, Deadlock Detection, Deadlock Recovery, Wait-for Graph, Wait-Die Scheme, Wound-Wait Scheme, Timestamp Based Protocol, Concurrency Control in DBMS, Transaction Management, Database Deadlock, Deadlock Conditions, DBMS Tutorial, Database Management System, SQL and DBMS, Computer Science Tutorial