Search notes:

Oracle: DBMS_LOCK

dbms_lock provides the interface to Oracle Lock Management services.
Locks that are created with dbms_lock have the prefix UL to prevent conflicts with DBMS locks.

Functions and procedures

allocate_unique Create a lockhandle from a «lock name». Calling this procedure commits the current transaction.
allocate_unique_autonomous Same as allocate_unique except that the transaction is commited autonomously and does therefore not commit the current transaction.
convert
release
request
sleep Wait for the given amount of seconds. dbms_lock.sleep is deprecated in favor of dbms_session.sleep (See also PLS-00201: identifier 'DBMS_LOCK' must be declared)

Example: Acquiring an exclusive lock

A session tries to acquire a lock named tq84 lock test in exclusive mode:
declare
   lockhandle  varchar2(128);
   ret         pls_integer;
begin
 
   dbms_lock.allocate_unique_autonomous(
       'tq84 lock test',
        lockhandle
   );
  
   ret := dbms_lock.request(
        lockhandle,
        dbms_lock.x_mode, -- exclusive access,
        timeout => 0
   );

   if    ret = 0 then
         dbms_output.put_line('Lock was sucessfully acquired');
   elsif ret = 1 then
         raise_application_error(-20800, 'Lock is already exclusively acquired, lockhandle = ' || lockhandle);
   else
         raise_application_error(-20800, 'Error! Ret = ' || ret);
   end if;
 
end;
/
If no other session has already acquired this lock in exclusive mode, this PL/SQL block prints
Lock was sucessfully acquired
If another session now executes the same block, the following error is thrown:
ORA-20800: Lock is already exclusively acquired, lockhandle = 10737418641073741864187
It's now possible to determine the session that owns the lock with the following statement:
select
   ses.sid,
   ses.username,
   ses.osuser,
   lck.ctime      secs_allocated,
   ses.program,
   ses.module
from
   v$lock    lck                             join
   v$session ses on lck.sid = ses.sid
where
   lck.type = 'UL' and
   lck.id1  = substr('10737418641073741864187', 1, 10);
The first session releases a lock like so:
declare
   lockhandle  varchar2(128);
   ret         pls_integer;
begin
 
   dbms_lock.allocate_unique_autonomous(
       'tq84 lock test',
        lockhandle
   );

   ret := dbms_lock.release(
        lockhandle
   );

   if ret != 0 then
      raise_application_error(-20800, 'Error! Ret = ' || ret);
   end if;
 
end;
/

See also

Examples for dbms_lock.
dbms_lock_allocated
Setting bit 32 in _qa_lrg_type seems to enable debugging in dbms_lock.
Oracle DBMS PL/SQL Packages

Index

Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 8 attempt to write a readonly database in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php:78 Stack trace: #0 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(78): PDOStatement->execute(Array) #1 /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php(30): insert_webrequest_('/notes/developm...', 1738303095, '18.225.54.199', 'Mozilla/5.0 App...', NULL) #2 /home/httpd/vhosts/renenyffenegger.ch/httpsdocs/notes/development/databases/Oracle/installed/packages/dbms/lock/index(139): insert_webrequest() #3 {main} thrown in /home/httpd/vhosts/renenyffenegger.ch/php/web-request-database.php on line 78