Oracle lock table and unlock
SELECT/* + rule */s. username,
Decode (l. type, 'TT', 'table lock ',
'Tx ', 'row lock ',
NULL) LOCK_LEVEL,
O. owner, o. object_name, o. object_type,
S. sid, s. serial #, s. terminal, s. machine, s. program, s. osuser
FROM v $ session s, v $ lock l, dba_objects o
WHERE l. sid = s. sid
AND l. id1 = o. object_id (+)
AND s. username is NOT Null
-- Kill session Statement
Alter system kill session '123 ';
-- The following are related tables:
SELECT * FROM v $ lock;
SELECT * FROM v $ sqlarea;
SELECT * FROM v $ session;
SELECT * FROM v $ process;
SELECT * FROM v $ locked_object;
SELECT * FROM all_objects;
SELECT * FROM v $ session_wait;
-- 1. Check the session information of the locked object and the name of the locked object.
SELECT l. session_id sid, s. serial #, l. locked_mode, l. oracle_username,
L. OS _user_name, s. machine, s. terminal, o. object_name, s. logon_time
FROM v $ locked_object l, all_objects o, v $ session s
WHERE l. object_id = o. object_id
AND l. session_id = s. sid
Order by sid, s. serial #;
-- 2. Locate the sid, serial #, OS _user_name, machine name, terminal, and executed STATEMENT OF THE session OF THE locked table.
-- More SQL _text and action than above
SELECT l. session_id sid, s. serial #, l. locked_mode, l. oracle_username, s. user #,
L. OS _user_name, s. machine, s. terminal, a. SQL _text, a. action
FROM v $ sqlarea a, v $ session s, v $ locked_object l
WHERE l. session_id = s. sid
AND s. prev_ SQL _addr = a. address
Order by sid, s. serial #;
-- 3. Identify the sid, serial #, OS _user_name, machine_name, terminal, type, mode of the lock.
SELECT s. sid, s. serial #, s. username, s. schemaname, s. osuser, s. process, s. machine,
S. terminal, s. logon_time, l. type
FROM v $ session s, v $ lock l
WHERE s. sid = l. sid
AND s. username IS NOT NULL
Order by sid;
This statement finds the locks generated by all the DML statements in the database,
Any DML statement actually produces two locks: one is the table lock and the other is the row lock.
Lock kill command
Alter system kill session 'sid, serial #'
SELECT/* + rule */s. username,
Decode (l. type, 'TT', 'table lock ',
'Tx ', 'row lock ',
NULL) LOCK_LEVEL,
O. owner, o. object_name, o. object_type,
S. sid, s. serial #, s. terminal, s. machine, s. program, s. osuser
FROM v $ session s, v $ lock l, dba_objects o
WHERE l. sid = s. sid
AND l. id1 = o. object_id (+)
AND s. username is NOT NULL
If a lock wait occurs, we may want to know who has locked the table and who has waited.
The following statement can be used to query who has locked the table and who is waiting.
The preceding query result is a tree structure. If a subnode exists, it indicates that a wait occurs.
If you want to know which rollback segment the lock uses, you can also associate it with V $ rollname. xidusn is the USN of the rollback segment.
Col user_name format a10
Col owner format a10
Col object_name format a10
Col object_type format a10
SELECT/* + rule */lpad ('', decode (l. xidusn, 0, 3, 0) | l. oracle_username User_name,
O. owner, o. object_name, o. object_type, s. sid, s. serial #
FROM v $ locked_object l, dba_objects o, v $ session s
WHERE l. object_id = o. object_id
AND l. session_id = s. sid
Order by o. object_id, xidusn DESC