Thursday 23 February 2012

DBMS_LOCK


The DBMS_LOCK package provides an interface to Oracle Lock Management services. You can request a lock of a specific mode, give it a unique name recognizable in another procedure in the same or another instance, change the lock mode, and release it.

DBMS Locks are allocated via

DBMS_LOCK.allocate_unique(lockname => c_lock_name, lockhandle => tmp_lock_handle); 

You cannot see them in V$lock and they stay in place through a Shutdown and Restart

You can find them in sys.dbms_lock_allocated

SQL> select * from sys.dbms_lock_allocated ;

NAME                   LOCKID EXPIRATIO
------------------ ---------- ---------
EOD_LOCK           1073741824 15-JUL-11

You can clear them by running:
DBMS_LOCK.release(id => 1073741824);

Reason I am posting this is that on the 15th of July 2011 one of our core systems failed during the End of Day process. (hardware error)

When it was eventually brought back up, the Business attempted to resume the End of Day but were told: "EOD already in progress"

This left the DBA on call (Me) scratching my head as there was nothing in v$lock and:

select sesion.sid,
       sql_text,
       sesion.username,
       sesion.OSUSER,
       sesion.process,
       sesion.MACHINE,
       sesion.PROGRAM
  from v$sqltext sqltext, v$session sesion
 where sesion.sql_hash_value = sqltext.hash_value
   and sesion.sql_address    = sqltext.address
 order by sid, sqltext.piece
/

was returning nothing.

Eventually after looking through the EOD package I discovered the DBMS_LOCK.
Pain.....


2 comments:

  1. This will not work:

    DBMS_LOCK.release(lockhandle => EOD_LOCK);

    You cannot release a lock by name. You CAN use the name to acquire a lockhandle then use that.



    declare
    LH varchar2(2000) ;
    begin
    dbms_lock.allocate_unique( 'EOD_LOCK', LH, 300 ) ;
    dbms_lock.release(lockhandle => LH ) ;
    end ;
    /

    ReplyDelete
  2. Doh!

    During that "Issue" I used the LOCK ID, Not the name
    Did not realise the other does not work

    Thanks for your comment :)

    I'll fix
    G.

    ReplyDelete