Deadlocks are one of the common concurrency-related issues Oracle DBAs encounter in production databases. They can cause application errors, transaction failures, and unexpected interruptions to business operations.
The important thing is that a deadlock is not necessarily caused by a database performance issue. In many cases, it happens because multiple transactions access and modify the same resources in a different order.
Here's a practical, DBA-focused blog covering how deadlocks occur, how Oracle detects them, and how to investigate and prevent them in production environments.
Introduction
In a production Oracle database, multiple users and applications access the same tables and records simultaneously. Oracle manages this concurrent access using locks to maintain data consistency and prevent conflicting changes.
Most of the time, locking happens in the background without any issues. However, there are situations where two or more transactions end up waiting for resources held by each other. When this happens, Oracle identifies the situation as a Deadlock.
Deadlocks can interrupt application transactions and generate errors such as `ORA-00060: deadlock detected while waiting for resource`. From a DBA perspective, identifying a deadlock is only the first step. The real task is to understand which sessions and database resources were involved, why the transactions entered a circular wait, and what changes can prevent the issue from happening again.
In this blog, let's understand how deadlocks occur in Oracle, how to identify them, and how to troubleshoot them from a practical DBA perspective.
What Is a Deadlock in Oracle Database?
A deadlock occurs when two or more transactions are waiting for resources locked by each other, creating a circular dependency.
In simple terms, one session holds a resource that another session needs, while the second session holds a resource required by the first session. Neither transaction can proceed because each is waiting for the other to release its lock.
For example, consider two sessions working on the same employee table.
- Session 1 updates employee ID 100 and acquires a lock on that row.
- Session 2 updates employee ID 200 and acquires a lock on that row.
- Session 1 then tries to update employee ID 200, which is already locked by Session 2.
- Session 2 tries to update employee ID 100, which is already locked by Session 1.
Now both sessions are waiting for each other to release their locks. This creates a deadlock.
Oracle automatically detects this condition and resolves it by rolling back one of the statements involved in the deadlock.
How Does a Deadlock Occur?
Let's understand the scenario with a practical SQL example.Assume we have an EMPLOYEES table containing two records:
| EMPLOYEE_ID | SALARY |
| ----------- | ------ |
| 100 | 50000 |
| 200 | 60000 |Session 1
UPDATE employees
SET salary = salary + 5000
WHERE employee_id = 100;The update succeeds, and Session 1 holds a row lock on employee ID 100. However, the transaction is not committed yet.
Session 2
UPDATE employees
SET salary = salary + 5000
WHERE employee_id = 200;This update also succeeds. Session 2 now holds a row lock on employee ID 200. Next, both sessions attempt to update the row locked by the other session.
Session 1
UPDATE employees
SET salary = salary + 5000
WHERE employee_id = 200;Session 1 waits because Session 2 holds the lock on employee ID 200.
Session 2
UPDATE employees
SET salary = salary + 5000
WHERE employee_id = 100;Session 2 also waits because Session 1 holds the lock on employee ID 100.
At this point, neither transaction can proceed.
Oracle detects the circular wait and raises the following error in one of the participating sessions:
ORA-00060: deadlock detected while waiting for resource Common Causes of Deadlocks in Oracle
Deadlocks can happen for different reasons depending on how applications execute transactions and access database objects.
Here are some common scenarios DBAs should look for.
Updating Rows in Different Orders
This is one of the most common causes of application-level deadlocks.
When two transactions update the same set of records but access them in different orders, they can end up waiting for each other's locks.
For example, one transaction updates employee 100 first and employee 200 next, while another transaction updates employee 200 first and employee 100 next.
Following a consistent order when updating records can significantly reduce this type of deadlock.
Long-Running Transactions
Transactions that remain open for a long time can hold row locks and increase the possibility of blocking and deadlocks.
For example, an application may update records and then perform additional processing before issuing a COMMIT.
During this period, other sessions attempting to modify the same records may have to wait.
DBAs should investigate applications that hold transactions open unnecessarily, especially when multiple sessions are accessing the same business data.
Triggers and Cascading Operations
Database triggers can introduce additional DML operations that are not immediately visible in the original SQL statement.
For example, an UPDATE on one table may fire a trigger that updates another table. If another transaction accesses these tables in the opposite order, a deadlock can occur.
During investigation, DBAs should check whether triggers or cascading operations are contributing to the locking sequence.
Unindexed Foreign Keys
Foreign key indexes are another area worth reviewing during deadlock and locking investigations.
When a parent key is updated or deleted, Oracle may need to acquire additional locks on the child table. If the foreign key column is not indexed, certain parent-table operations can acquire restrictive table-level locks on the child table. This can increase locking contention and contribute to complex blocking situations.
However, an unindexed foreign key does not automatically mean a deadlock will occur. The actual impact depends on the concurrent operations and transaction sequence.
DBAs should review foreign key indexes when investigating related locking issues.
How Does Oracle Detect and Resolve Deadlocks?
Oracle Database has an automatic deadlock detection mechanism. DBAs do not need to manually identify circular waits for Oracle to resolve them.
When Oracle detects a deadlock, it rolls back one of the statements involved in the deadlock and releases the locks acquired by that statement.
The affected session receives:
ORA-00060: deadlock detected while waiting for resourceOracle also generates diagnostic information in a trace file, which contains details about the transactions and resources involved.
One important distinction is that Oracle does not automatically roll back the entire transaction. The application must handle the error appropriately and decide whether to retry the operation or roll back the remaining transaction.
Simply retrying the failed statement without considering the state of the transaction may not be sufficient.
How to Identify Deadlocks in Oracle Database
As a DBA, the first step in investigating an ORA-00060 error is to identify the corresponding trace file.
The alert log records deadlock errors, and Oracle generates diagnostic information in the associated trace file.
Check the Alert Log
You can use the following query to identify ORA-00060 messages from the alert log:
SELECT
originating_timestamp,
message_text
FROM v$diag_alert_ext
WHERE message_text LIKE '%ORA-00060%'
ORDER BY originating_timestamp DESC;This helps identify when deadlock errors occurred.
Locate the Trace File
Use the following query to identify the diagnostic trace directory:
SELECT name, value
FROM v$diag_info
WHERE name IN ('Diag Trace', 'Default Trace File');The `Diag Trace` entry provides the trace directory for the current instance.
The deadlock trace file contains important information such as:
- Sessions involved in the deadlock.
- Transaction details.
- Database resources involved.
- Lock information.
- Deadlock graph.
The deadlock graph is particularly useful because it helps identify which resources were held and which resources each transaction was waiting for.
Understand the Deadlock Graph
When reviewing the trace file, look for the section containing:
DEADLOCK DETECTEDThe deadlock graph provides details about the resources and sessions involved in the circular wait.
As a DBA, focus on identifying the sessions involved, the resource types, the lock modes, and the SQL statements associated with the transactions.
Do not rely only on the ORA-00060 message. The trace file is usually the most useful starting point for understanding the actual cause.
How to Check Blocking Sessions in Oracle
Although a deadlock is generally resolved quickly by Oracle, checking current blocking sessions can help identify related locking activity.
The following query helps identify sessions waiting for another session:
SELECT
sid,
serial#,
username,
blocking_session,
event,
wait_class,
seconds_in_wait,
sql_id
FROM v$session
WHERE blocking_session IS NOT NULL;This query provides information about sessions currently waiting for a blocking session.
You can use it to investigate ongoing blocking situations, but remember that a deadlock may already have been resolved by the time you execute the query.
For historical deadlock analysis, the trace file should remain the primary source of information.
How to Troubleshoot Deadlocks: A Practical DBA Approach
When an ORA-00060 error is reported in a production environment, I would follow a structured troubleshooting approach rather than immediately terminating sessions or making database-level changes.
Step 1: Identify the occurrence time
Start by checking the alert log for the ORA-00060 error. Note the timestamp and identify whether the issue occurred once or repeatedly.
Step 2: Locate and analyze the trace file
Find the corresponding trace file from the ADR trace directory. Review the deadlock graph to understand which sessions, transactions, and resources were involved.
Step 3: Identify the SQL statements
Review the SQL statements associated with the transactions. Check which rows or database objects were being modified and identify the order in which the operations were executed.
Step 4: Review application transaction logic
Check whether different application processes are updating the same records in different orders. Review triggers, cascading operations, and transaction boundaries where relevant.
Step 5: Check related locking issues
Review whether long-running transactions, unindexed foreign keys, or other concurrent DML operations are contributing to the issue.
Step 6: Work with the application team
In many cases, the permanent solution requires changes to application transaction logic rather than database parameter modifications.
Share the deadlock graph and relevant SQL statements with the development team so the transaction sequence can be reviewed.
Step 7: Monitor after implementing the fix
After the required changes are implemented, monitor the alert log and application error logs to confirm whether the deadlock continues to occur.
How to Prevent Deadlocks in Oracle Database
Preventing deadlocks is mainly about ensuring that concurrent transactions access database resources in a consistent manner.
One of the most effective approaches is to maintain a consistent order when updating multiple records. If different application processes update the same set of records, they should follow the same sequence wherever possible.
Keep transactions as short as practical and avoid holding locks while performing unnecessary application processing. Applications should commit or roll back transactions at appropriate points instead of keeping them open longer than required.
DBAs should also review database triggers and cascading DML operations to ensure they are not introducing unexpected locking dependencies. Foreign key indexing should be evaluated based on the application's DML workload, particularly when parent-table updates and deletes are involved.
Another important practice is implementing proper exception handling in applications. When an ORA-00060 error occurs, the application should handle the failed statement and transaction state correctly before attempting another operation.
Finally, monitor recurring ORA-00060 errors and investigate their root causes instead of treating each occurrence as an isolated incident.
Deadlocks are a common possibility in Oracle databases where multiple transactions access and modify shared resources concurrently. Although Oracle automatically detects deadlocks and resolves them by rolling back the affected statement, repeated occurrences can impact application stability and transaction processing.
From a DBA perspective, deadlock management should be part of regular database monitoring and performance troubleshooting. Monitoring ORA-00060 errors through the alert log, reviewing the corresponding trace files, and analyzing deadlock graphs help identify the SQL statements, sessions, and database objects involved. Keeping track of recurring patterns, long-running transactions, foreign key indexes, and trigger-related operations can also help narrow down the root cause.
However, resolving deadlocks is not always a database-side fix. Many deadlocks originate from application transaction design, particularly when multiple sessions access the same records in different orders. Working closely with application teams to improve transaction handling, maintain consistent row access order, and minimize unnecessary locking is essential for preventing recurring issues.
Also, avoid blindly terminating sessions whenever a deadlock occurs. Oracle already has an automatic deadlock detection and resolution mechanism, so the focus should be on understanding the underlying issue rather than treating every occurrence as an isolated incident.
Ultimately, effective deadlock management is not just about resolving ORA-00060 errors but identifying their root cause, improving transaction handling, and ensuring stable database operations.
Comments
Post a Comment