Oracle Row-Level Locking ↔ Distributed Transactions: Lock Release Timing
0 reputation · 18 Aug 2022, 01:58 UTC
0 reputation · 18 Aug 2022, 01:58 UTC
Oracle Database utilizes Interested Transaction Lists (ITL) in data block headers to manage row-level locking. While standard local transactions release locks immediately upon a COMMIT or ROLLBACK, distributed transactions utilizing the XA standard involve a two-phase commit (2PC) process.
In a distributed environment, a transaction may enter a 'prepared' state where the local resource manager has agreed to commit but is awaiting the final global decision from the transaction manager. This creates a window of uncertainty regarding the exact moment row-level locks are released to other concurrent sessions.
When a second session attempts to access a locked row using SELECT ... FOR UPDATE NOWAIT, the behavior is typically a prompt ORA-00054 error. However, the latency between the local phase-one commit and the final global commit can lead to unpredictable lock durations.
NOWAIT mechanism differentiate between a standard active transaction lock and a lock held by a distributed transaction in a prepared state?32275 reputation · 18 Aug 2022, 13:38 UTC
Yes – in an Oracle XA distributed transaction the row‑level lock (and the ITL slot that protects it) is **not** released after the phase‑one xa_prepare. The lock persists for the entire "prepared" (in‑doubt) interval and is released only when the transaction manager later issues the global xa_commit or xa_rollback to the local branch.
xa_prepare; they survive instance crash and recovery, appearing in DBA_2PC_PENDING until the coordinator resolves the branch.SELECT … FOR UPDATE NOWAIT raises ORA-00054 whenever the lock cannot be obtained immediately, irrespective of whether the holder is a normal active transaction, an active XA branch, or a prepared XA branch. The error does not indicate the lock’s source.The window between xa_prepare and the final xa_commit/xa_rollback is dictated by the transaction manager’s coordination logic. If the coordinator is slow, unreachable, or decides heuristically, the lock may be held for seconds, minutes, or longer.
xa_prepare.SELECT … FOR UPDATE NOWAIT on the same row; you should see ORA-00054.SELECT * FROM DBA_2PC_PENDING WHERE LOCAL_TRAN_ID = :xid;
V$LOCK / V$TRANSACTION with V$SESSION to see the enqueue; note that the preparing branch will have no active session attached.xa_commit (or xa_rollback) from the transaction manager.SELECT … FOR UPDATE NOWAIT; it should now succeed immediately, showing the lock was released at the global decision point.Since NOWAIT does not differentiate lock sources, you must rely on the data dictionary views:
DBA_2PC_PENDING – shows prepared/in‑doubt branches.V$LOCK with TYPE='TX' – identifies the transaction holding the row lock.V$SESSION – if a session is attached, it is a normal active transaction; absence of a session indicates a prepared XA branch.If you observe a lock held by a branch with no attached session and an entry in DBA_2PC_PENDING, the blocker is a prepared distributed transaction.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.