Oracle Lock Conflicts with dba_lock and SQL*Plus Format
Oracle Database: Understanding Lock Conflicts with dba_lock and Column Formatting
Purpose
DBA_LOCK lists all locks or latches held in the database, and all outstanding requests for a lock or latch. This query reads that view to show which sessions hold or want each lock, the lock modes involved, and whether a lock is blocking other sessions. SQL*Plus COLUMN commands set the display width of the text columns.
Code
1column lock_type format a12
2column mode_held format a10
3column mode_requested format a10
4column blocking_others format a20
5column username format a10
6SELECT session_id
7, lock_type
8, mode_held
9, mode_requested
10, blocking_others
11, lock_id1
12FROM dba_lock l
13WHERE lock_type NOT IN ('Media Recovery', 'Redo Thread')
14/
Code Breakdown
column lock_type format a12column mode_held format a10column mode_requested format a10column blocking_others format a20column username format a10
These lines are SQL*Plus commands, not part of the query. The SQL*Plus COLUMN command sets display attributes for a column, and FORMAT An changes the width of a character column to n. The username line has no effect here, because the query does not select a username column.
SELECT session_id, lock_type, mode_held, mode_requested, blocking_others, lock_id1: Reads six columns from thedba_lockview.FROM dba_lock l: Names the data source. The aliaslis optional in a single-table query.WHERE lock_type NOT IN ('Media Recovery', 'Redo Thread'): Removes two internal lock types, media recovery and redo thread, from the output. Other system lock types still appear.
Key Points
session_id: The session holding or acquiring the lock.lock_type: The lock type, shown as a name. The query's own filter compares it to names such as 'Media Recovery' and 'Redo Thread'. The Oracle Enqueue Names appendix lists each two-letter enqueue code with its name, for example TM (DML enqueue) and TX (transaction).mode_held: The lock mode the session holds.mode_requested: The lock mode the session has requested.blocking_others: Shows whether the lock is currently blocking others.lock_id1: A type-specific lock identifier, part 1. What it identifies depends on the lock type.DBA_LOCKalso hasLOCK_ID2, the second part.
What the Output Tells You
The query lists every lock in DBA_LOCK except the two filtered types. For each lock it shows:
- The session that holds or wants the lock.
- The lock type and the modes held and requested.
- Whether the lock is blocking other sessions.
The query does not pair each waiting session with the session that blocks it. For that, DBA_WAITERS shows all the sessions that are waiting for a lock, with the holding session beside each waiting session.
After you find a blocking session, the usual options are:
- Change application code so transactions hold locks for less time.
- End the blocking session with
ALTER SYSTEM KILL SESSION, after you confirm what the session is doing. - Review the design of the tables and transactions involved, to reduce contention.
References
- DBA_LOCK - Oracle Database Reference 21c - the column list and descriptions for the view this query reads.
- Oracle Enqueue Names - Oracle Database Reference 21c - the list of lock types that
DBA_LOCKlinks to. - V$LOCK - Oracle Database Reference 21c - the dynamic performance view of locks, with lock modes as numbers and the
BLOCKcolumn values. - DBA_WAITERS - Oracle Database Reference 21c - pairs each waiting session with its holding session.
- COLUMN - SQL*Plus User's Guide and Reference 21c - the
FORMAT Anoption used to set column widths. - Killing Oracle Sessions - oracle-base.com - syntax and behavior of
ALTER SYSTEM KILL SESSION.