SoFunction
Updated on 2025-03-03

Oracle database query table is locked in various ways

In Oracle database, query tables are locked in a variety of ways. Here are some commonly used methods to query table locks in Oracle database:

1. Use V$LOCKED_OBJECT view

V$LOCKED_OBJECT is a dynamic performance view provided by Oracle, which is used to display the currently locked object information. By querying this view, you can quickly determine which tables are locked.

SELECT object_name, object_type  
FROM v$locked_object  
WHERE object_type = 'TABLE';

If the query result contains the table name you care about, then the table is currently locked.

2. Use DBA_OBJECTS and DBA_LOCKS views

Although the DBA_OBJECTS view itself does not directly display lock information, it can be used in combination with the DBA_LOCKS view to indirectly determine whether the table is locked. DBA_LOCKS provides information about locks in the current database.

SELECT o.object_name, l.lock_type  
FROM dba_objects o  
JOIN dba_locks l ON o.object_id = l.object_id  
WHERE o.object_type = 'TABLE'  
AND o.object_name = 'Your table name';

If there is a corresponding table name and lock type in the query result, it means that the table is locked.

3. Use VSESSION and VLOCK views

By jointly querying the VSESSION and VLOCK views, you can view the session information holding the lock, thereby determining whether the table is locked.

SELECT , #, , l.object_id, o.object_name  
FROM v$session s  
JOIN v$lock l ON  =   
JOIN dba_objects o ON l.object_id = o.object_id  
WHERE o.object_type = 'TABLE'  
AND o.object_name = 'Your table name';

This query will list the session information that holds the table lock.

4. Use DBA_BLOCKERS and DBA_WAITERS views

These two views are used to display information about blocking sessions and waiting sessions, respectively. By querying these two views, you can understand which sessions are waiting for or holding a lock on the table.

SELECT  AS blocking_session,  
        AS waiting_session,  
       o.object_name  
FROM dba_blockers b  
JOIN dba_waiters w ON  = w.blocking_session  
JOIN dba_objects o ON w.object_id = o.object_id  
WHERE o.object_type = 'TABLE'  
AND o.object_name = 'Your table name';

If there is data in the query result, it means that a session is waiting for the lock of the table, and a session also holds the lock of the table.

5. Use V$LOCK view to query directly

The V$LOCK view provides more detailed information about the current database lock, including the type, pattern, etc. of the lock.

SELECT sid, type, lmode, request, id1, id2  
FROM v$lock  
WHERE id1 IN (SELECT object_id FROM dba_objects WHERE object_name = 'Your table name' AND object_type = 'TABLE');

This query will list lock information related to the specified table.

Summarize

Oracle provides a variety of views and methods to query table lock situations. In practical applications, the most suitable method can be selected according to specific needs to check whether the table is locked. At the same time, knowing the type of lock and the session information that holds the lock is crucial to solving lock problems.

This is the end of this article about the implementation of various ways of locking oracle database query tables. For more related contents of locked oracle query tables, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!