The below query helps to identify the session that has locked the resource that your session is trying to lock.
[oracle@rac2 ~]$
sqlplus / as sysdba
SELECT OBJECT_ID, SESSION_ID, inst_id FROM GV$LOCKED_OBJECT
WHERE OBJECT_ID=(select object_id
FROM dba_objects
where object_name='LOCK_TABLE' and object_type='TABLE' AND OWNER='SH');
select s.sid, s.serial#,s.inst_id, s.program,p.spid from gv$session s , gv$process p
where p.addr =s.paddr
and s.sid in (46);
-- OR --
set linesize 140
set pages 100
col username format a20
col "SID,SESSION#" format a20
col sess_id format a10
col object format a30
col mode_held format a10
select oracle_username || ' (' || s.osuser || ')' username
, s.sid || ',' || s.serial# "SID,SESSION#"
, owner || '.' || object_name object
, object_type
, decode( l.block
, 0, 'Not Blocking'
, 1, 'Blocking'
, 2, 'Global') status
, decode(v.locked_mode
, 0, 'None'
, 1, 'Null'
, 2, 'Row-S (SS)'
, 3, 'Row-X (SX)'
, 4, 'Share'
, 5, 'S/Row-X (SSX)'
, 6, 'Exclusive', TO_CHAR(lmode)) mode_held
from v$locked_object v
, dba_objects d
, v$lock l
, v$session s
where v.object_id = d.object_id
and v.object_id = l.id1
and v.session_id = s.sid
order by oracle_username,session_id;
To identify the locked rows, use the below query:
set lines 200
col object_name for a30
select do.object_name
, row_wait_obj#
, row_wait_file#
, row_wait_block#
, row_wait_row#
, dbms_rowid.rowid_create (1, ROW_WAIT_OBJ#, ROW_WAIT_FILE#,
ROW_WAIT_BLOCK#, ROW_WAIT_ROW#) ROW_ID
from v$session s
, dba_objects do
where s.ROW_WAIT_OBJ# = do.OBJECT_ID
and do.object_name=upper('&OBJECT_NAME_WITHOUT_SCHEMA_NAME')
;
-- OR --
SELECT
O.OBJECT_NAME,
S.SID,
S.SERIAL#,
P.SPID,
S.PROGRAM,
SQ.SQL_FULLTEXT,
S.LOGON_TIME
FROM
V$LOCKED_OBJECT L,
DBA_OBJECTS O,
V$SESSION S,
V$PROCESS P,
V$SQL SQ
WHERE
L.OBJECT_ID = O.OBJECT_ID
AND L.SESSION_ID = S.SID
AND S.PADDR = P.ADDR
AND S.SQL_ADDRESS = SQ.ADDRESS;
SQL>
SELECT OBJECT_ID, SESSION_ID, inst_id FROM GV$LOCKED_OBJECT
WHERE OBJECT_ID=(select object_id
FROM dba_objects
where object_name='LOCK_TABLE' and object_type='TABLE' AND OWNER='SH');
2 3 4
OBJECT_ID SESSION_ID INST_ID
---------- ---------- ----------
82844
46
1
<--- Please note session ID 46
SQL>
select s.sid, s.serial#,s.inst_id, s.program,p.spid from gv$session s , gv$process p
where p.addr =s.paddr
and s.sid in (
46
);
2 3
SID SERIAL# INST_ID PROGRAM SPID
---------- ---------- ---------- ------------------------------------------------ --------
46 11
1 [email protected] (TNS V1-V3) 5768
SQL>