Understanding Distributed Transaction Locks and Session-Specific Data in Oracle
When working with Oracle databases, developers and DBAs often encounter the error:
“Distributed transaction waiting for lock”
This usually happens when a transaction spanning multiple databases is waiting for a resource or lock. Understanding how Oracle handles session-specific data, locks, and distributed transactions is crucial to troubleshoot and prevent this issue.
1️⃣ What Is a Distributed Transaction?
A distributed transaction is a transaction that:
Involves more than one database (via DB Links).
Requires all participating databases to commit or rollback together (two-phase commit).
Oracle manages this using 2PC (Two-Phase Commit):
Prepare Phase – each database ensures it can commit.
Commit Phase – the transaction is committed on all databases.
If a session is blocked on any database during this process, you may see:
Distributed transaction waiting for lock
2️⃣ Common Causes of Distributed Transaction Locks
Cause Explanation
Remote row locked Another session on a remote DB holds the row your transaction wants to update.
Long-running transactions Transactions kept open for too long increase lock wait chances.
Inconsistent locking order Different sessions updating tables in reverse order can deadlock.
Unresolved 2PC transactions In-doubt transactions that didn’t commit or rollback fully.
Mixing DML and SELECT FOR UPDATE Locks rows on the remote DB until the transaction completes.
Temporary tablespace issues Insufficient space can prevent proper processing of temporary data.
3️⃣ Session-Specific Data in Oracle
When a session manipulates temporary or session-specific data, Oracle uses:
Global Temporary Tables (GTT)
Structure: Permanent in the database.
Data: Session-specific, stored in temporary tablespace.
Lifetime:
ON COMMIT DELETE ROWS → cleared after each commit.
ON COMMIT PRESERVE ROWS → persists until session ends.
Example:
CREATE GLOBAL TEMPORARY TABLE temp_orders (
order_id NUMBER,
amount NUMBER
) ON COMMIT PRESERVE ROWS;
Each session sees only its data.
Data is automatically cleaned by Oracle when the session ends or commits (depending on definition).
4️⃣ Tracking Locks and Distributed Transactions
Oracle provides several views to monitor session locks and distributed transactions:
Purpose View
Active sessions V$SESSION
Locks held/requested V$LOCK
Active transactions V$TRANSACTION
Pending / in-doubt distributed transactions DBA_2PC_PENDING
RAC-specific sessions/locks GV$SESSION, GV$LOCK
Top Queries for Checking Distributed Transaction Locks:
1️⃣ Check sessions waiting on locks
SELECT s.sid, s.serial#, s.username, s.program, s.status, s.event,
l.type AS lock_type, l.id1, l.id2, l.lmode AS lock_mode_held,
l.request AS lock_mode_requested, l.block AS is_blocking
FROM v$session s
JOIN v$lock l ON s.sid = l.sid
WHERE s.status='ACTIVE' AND (l.block=1 OR l.request>0);
2️⃣ Check in-doubt distributed transactions
SELECT LOCAL_TRAN_ID, GLOBAL_TRAN_ID, STATE, INITIATOR, DB_LINK, CREATE_DATE
FROM DBA_2PC_PENDING
ORDER BY CREATE_DATE DESC;
3️⃣ Check sessions waiting on distributed locks
SELECT sid, serial#, username, program, status, event, seconds_in_wait, blocking_session
FROM v$session
WHERE status='ACTIVE' AND event LIKE '%distributed%';
5️⃣ Flowchart: Session, GTT, Locks, and Distributed Transactions
Here’s a simplified flow of how session-specific data and locks interact in Oracle:
+-------------------------+
| Oracle Session |
| (User Login / SID) |
+-------------------------+
|
| DML on temporary or regular tables
v
+-------------------------+
| Global Temporary Table |
| (Data session-specific)|
+-------------------------+
|
| Stored in
v
+-------------------------+
| Temporary Tablespace |
| (Temporary segments) |
+-------------------------+
|
| Transaction ongoing
v
+-------------------------+
| V$SESSION / V$LOCK |
| Shows session info & |
| locks held/requested |
+-------------------------+
|
| Distributed transaction?
v
+-------------------------+
| DBA_2PC_PENDING |
| (Tracks in-doubt or |
| pending distributed |
| transactions) |
+-------------------------+
6️⃣ Best Practices to Avoid Distributed Transaction Locks
Keep distributed transactions short.
Always commit frequently to release locks.
Maintain a consistent order of DML across databases.
Avoid long-running SELECT FOR UPDATE on remote tables.
Monitor DBA_2PC_PENDING and V$SESSION for blockers.
Use Global Temporary Tables for session-specific data to avoid interfering with permanent data.
✅ Conclusion
Distributed transaction locks in Oracle are often caused by:
Conflicts between sessions across databases.
Long-running transactions or in-doubt 2PC states.
Mismanagement of session-specific data.
By using GTTs, monitoring locks via V$LOCK/V$SESSION, and keeping transactions short, you can avoid most distributed transaction waiting issues.
Top comments (0)