Wednesday, May 18, 2011

Clearing a lock in Oracle

I've been using Oracle's SQLDeveloper now for a while and found you can easily lock a table by not committing an update. Coming from a MySQL and MSSQL world I'm pretty used to updates committing by themselves. Here is how I cleared the lock:


Run this SQL to see what's locking what:

SELECT SUBSTR(TO_CHAR(session_id),1,5) "SID",
       SUBSTR(lock_type,1,15) "Lock Type",
       SUBSTR(mode_held,1,15) "Mode Held",
       SUBSTR(blocking_others,1,15) "Blocking?"
  FROM dba_locks


Use this command to get the serial number of the session:

SELECT s.inst_id,
       s.sid,
       s.serial#,
       p.spid,
       s.username,
       s.program
FROM   gv$session s
       JOIN gv$process p ON p.addr = s.paddr AND p.inst_id = s.inst_id
WHERE  s.type != 'BACKGROUND';



Use this command to kill the session that's holding the lock:

ALTER SYSTEM KILL SESSION ',';
e.g.
ALTER SYSTEM KILL SESSION '126,19703';

Some good references:

http://www.oracle-base.com/articles/misc/KillingOracleSessions.php
http://www.broadh2o.net/docs/database/oracle/oracleLocks.html

No comments:

Followers