47

I need execute an SQL query in ORACLE it takes a certain amount of time. So I wrote this function:

CREATE OR REPLACE FUNCTION MYSCHEMA.TEST_SLEEP
(
TIME_  IN  NUMBER
)
RETURN INTEGER IS
 BEGIN
   DBMS_LOCK.sleep(seconds => TIME_);
RETURN 1;
 EXCEPTION
   WHEN OTHERS THEN
   RAISE;
   RETURN 1;
END TEST_SLEEP;

and I call in this way

SELECT TEST_SLEEP(10.5) FROM DUAL

but to work I need set grant of DBMS_LOCK to the owner of the procedure.

How I can rewrite this function without using the DBMS_LOCK.sleep function?

7
  • 2
    stupid question, but what is the behaviour expected (or necessity) of such a thing on the DB? When do you need that? Commented Apr 1, 2010 at 15:51
  • 2
    FYI.. Your exception block is pointless. The RETURN 1 in the block will never be reached, and if the block was completely missing... the exception would raise anyway. Commented Apr 2, 2010 at 1:13
  • I needed a sleep function on one occasion. In this app. an external program calls a database function that writes data to a file. The external program then accesses this file. On one particular system we needed a sleep at the end of the function because otherwise the external program would try to access the file before it was completely written. On most systems it worked without any built in delay.
    – Rene
    Commented Apr 2, 2010 at 10:49
  • So the external program isn't waiting for the function to return - in which case the sleep might not help? Or the function is returning before it's finished writing the file? If the latter then is it not flushing the data or explicitly closing the file - can't get my head around how this can be happening otherwise, maybe I'm missing something...
    – Alex Poole
    Commented Apr 2, 2010 at 15:34
  • 3
    Why don't you want to use the DBMS_LOCK.SLEEP() function? So many database applications are sabotaged by needless re-inventions of nuilt-in functionality.
    – APC
    Commented Apr 4, 2010 at 19:18

13 Answers 13

61

Short of granting access to DBMS_LOCK.sleep, this will work but it's a horrible hack:

IN_TIME INT; --num seconds
v_now DATE;

-- 1) Get the date & time 
SELECT SYSDATE 
  INTO v_now
  FROM DUAL;

-- 2) Loop until the original timestamp plus the amount of seconds <= current date
LOOP
  EXIT WHEN v_now + (IN_TIME * (1/86400)) <= SYSDATE;
END LOOP;
5
  • 6
    @Salvador: There's likely to be better answers - this one works, but I wouldn't expect the performance to be that great.
    – OMG Ponies
    Commented Apr 1, 2010 at 16:31
  • 8
    I agree with vincent.. this will work, but its a horrible horrible idea. Commented Apr 2, 2010 at 8:38
  • 6
    equal is dangerous, you can run into a skipped the value problem. You should use a <
    – Kingpin2k
    Commented Dec 12, 2015 at 2:39
  • 4
    Arg!!! OMG!!! This could degrade the performance of the entire database because it will consume a lot of CPU!!! We had a problem in a huge corporate environment thanks to "solutions" like this. I do not recommend. In fact, it is easier to blackmail your database administrator to gain this grant: hey, or you give me this grant or I will use an infinite loop... whatever you want. Definititely this will work.
    – Raul Luna
    Commented Sep 29, 2017 at 9:30
  • 1
    Just to elaborate : I was trying to create a bad PROC here - for testing purposes.
    – monojohnny
    Commented Jun 19, 2018 at 12:56
24

Create a procedure which just does your lock and install it into a different user, who is "trusted" with dbms_lock ( USERA ), grant USERA access to dbms_lock.

Then just grant USERB access to this function. They then wont need to be able to access DBMS_LOCK

( make sure you don't have usera and userb in your system before running this )

Connect as a user with grant privs for dbms_lock, and can create users

drop user usera cascade;
drop user userb cascade;
create user usera default tablespace users identified by abc123;
grant create session to usera;
grant resource to usera;
grant execute on dbms_lock to usera;

create user userb default tablespace users identified by abc123;
grant create session to userb;
grant resource to useb

connect usera/abc123;

create or replace function usera.f_sleep( in_time number ) return number is
begin
 dbms_lock.sleep(in_time);
 return 1;
end;
/

grant execute on usera.f_sleep to userb;

connect userb/abc123;

/* About to sleep as userb */
select usera.f_sleep(5) from dual;
/* Finished sleeping as userb */

/* Attempt to access dbms_lock as userb.. Should fail */

begin
  dbms_lock.sleep(5);
end;
/

/* Finished */
1
  • This is definitely the right approach to take. Apart from granting resource and create session to USERA; this account needs neither privilege.
    – APC
    Commented Apr 4, 2010 at 19:16
23

From Oracle 18c you could use DBMS_SESSION.SLEEP procedure:

This procedure suspends the session for a specified period of time.

DBMS_SESSION.SLEEP (seconds  IN NUMBER)

DBMS_SESSION.sleep is available to all sessions with no additional grants needed. Please note that DBMS_LOCK.sleep is deprecated.

If you need simple query sleep you could use WITH FUNCTION:

WITH FUNCTION my_sleep(i NUMBER)
RETURN NUMBER IS
BEGIN
    DBMS_SESSION.sleep(i);
    RETURN i;
END;
SELECT my_sleep(3) FROM dual;

db<>fiddle demo

11

There is a good article on this topic: PL/SQL: Sleep without using DBMS_LOCK that helped me out. I used Option 2 wrapped in a custom package. Proposed solutions are:

Option 1: APEX_UTIL.sleep

If APEX is installed you can use the procedure “PAUSE” from the publicly available package APEX_UTIL.

Example – “Wait 5 seconds”:

SET SERVEROUTPUT ON ;
BEGIN
    DBMS_OUTPUT.PUT_LINE('Start ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
    APEX_UTIL.PAUSE(5);
    DBMS_OUTPUT.PUT_LINE('End   ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
END;
/

Option 2: java.lang.Thread.sleep

An other option is the use of the method “sleep” from the Java class “Thread”, which you can easily use through providing a simple PL/SQL wrapper procedure:

Note: Please remember, that “Thread.sleep” uses milliseconds!

--- create ---
CREATE OR REPLACE PROCEDURE SLEEP (P_MILLI_SECONDS IN NUMBER) 
AS LANGUAGE JAVA NAME 'java.lang.Thread.sleep(long)';

--- use ---
SET SERVEROUTPUT ON ;
BEGIN
    DBMS_OUTPUT.PUT_LINE('Start ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
    SLEEP(5 * 1000);
    DBMS_OUTPUT.PUT_LINE('End   ' || to_char(SYSDATE, 'YYYY-MM-DD HH24:MI:SS'));
END;
/
1
  • Option 2 doesn't work for me - ORA-29548: Java system class reported: could not identify release specified in classes.bin
    – Betlista
    Commented Jan 17, 2022 at 17:02
9

If executed within "sqlplus", you can execute a host operating system command "sleep" :

!sleep 1

or

host sleep 1
1
5

What's about Java code wrapped by a procedure? Simple and works fine.

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED SNOOZE AS
public final class Snooze {
  private Snooze() {
  }
  public static void snooze(Long milliseconds) throws InterruptedException {
      Thread.sleep(milliseconds);
  }
}

CREATE OR REPLACE PROCEDURE SNOOZE(p_Milliseconds IN NUMBER) AS
    LANGUAGE JAVA NAME 'Snooze.snooze(java.lang.Long)';
2

It would be better to implement a synchronization mechanism. The easiest is to write a file after the first file is complete. So you have a sentinel file.

So the external programs looks for the sentinel file to exist. When it does it knows that it can safely use the data in the real file.

Another way to do this, which is similar to how some browsers do it when downloading files, is to have the file named base-name_part until the file is completely downloaded and then at the end rename the file to base-name. This way the external program can't "see" the file until it is complete. This way wouldn't require rewrite of the external program. Which might make it best for this situation.

2

You can use the DBMS_ALERT package as follows:

CREATE OR REPLACE FUNCTION sleep(seconds IN NUMBER) RETURN NUMBER
AS
    PRAGMA AUTONOMOUS_TRANSACTION;
    message VARCHAR2(200);
    status  INTEGER;
BEGIN
    DBMS_ALERT.WAITONE('noname', message, status, seconds);
    ROLLBACK;
    RETURN seconds;
END;
SELECT sleep(3) FROM dual;
1

If Java is installed on your 11G then you can do it in a java class and call it from your PL/SQL, but I am not sure that it does not require also a specific grant to call java.

2
  • Not sure why this was downvoted - this seems an ok suggestion to me.
    – monojohnny
    Commented Jul 14, 2015 at 15:44
  • @monojohnny Probably because getting granted permissions to create Java functions is not easier that getting permissions to DBMS_LOCK.SLEEP. Sure, it's theoretically possible, but it's almost certainly not practical. There's also another answer saying the same thing, only with actual example code.
    – jpmc26
    Commented Jun 18, 2018 at 20:17
1

You can use DBMS_PIPE.SEND_MESSAGE with a message that is too large for the pipe, for example for a 5 second delay write XXX to a pipe that can only accept one byte using a 5 second timeout as below

dbms_pipe.pack_message('XXX');<br>
dummy:=dbms_pipe.send_message('TEST_PIPE', 5, 1);

But then that requires a grant for DBMS_PIPE so perhaps no better.

0

Seems the java procedure/function could work. But why don't you compile your function under a user like the application schema or a admin account that has this grant and just grant your developer account execute on it. That way the definer rights are used.

-1

this is my solution:

declare
   vFecIni date;
   vFecFin date;
   vInicio number;
   vFinal  number;

   function milisegundos return number is
      vRes number;
   begin
      select round(
              ( (horas         * 3600000000)
              + (minutos       *   60000000)
              + (segundos      *    1000000)
              + microsegundos ) / 1000 )
         into vRes
         from ( select to_number(substr(tiempo, 1,2)) as horas,
                       to_number(substr(tiempo, 4,2)) as minutos,
                       to_number(substr(tiempo, 7,2)) as segundos,
                       to_number(substr(tiempo,  10)) as microsegundos
                 from ( select to_char(ct, 'HH24:MI:SS.FF') as tiempo
                          from (select current_timestamp as ct from dual) ) );
      
      return vRes;
      
   end milisegundos;
   
   function ms_to_char(pMs number) return varchar2 is
       CMILIS constant number:= 1000;
       CMINSEC constant number:= 60;
       CHORMIN constant number:= 60;
       vHoras number:= 0;
       vMinutos number:= 0;
       vSegundos number:= 0;
       vMilisegundos number:= 0;
       vResto number:= 0;
       vRes varchar2(32767);
    begin
    --
    -- Milisegundos
       vMilisegundos:= pMs;
       
    --
    -- Segundos
       if CMILIS < vMilisegundos then
          vResto:= mod(vMilisegundos, CMILIS);
          vSegundos:= (vMilisegundos - vResto) / CMILIS;
          vMilisegundos:= vResto;
          
       --
       -- Minutos
          if CMINSEC < vSegundos then
             vResto:= mod(vSegundos, CMINSEC);
             vMinutos:= (vSegundos - vResto) / CMINSEC;
             vSegundos:= vResto;
             
          --
          -- Horas
             if CHORMIN < vMinutos then
                vResto:= mod(vMinutos, CHORMIN);
                vHoras:= (vMinutos - vResto) / CHORMIN;
                vMinutos:= vResto;
              
             end if; -- Horas
          
          end if; -- Minutos
          
       end if; -- Segundos
       
    --
    -- Resultado en formato texto: HH24:MI:SS.FF
       vRes:= lpad(vHoras,        2, '0') || ':'
           || lpad(vMinutos,      2, '0') || ':'
           || lpad(vSegundos,     2, '0') || '.'
           || lpad(vMilisegundos, 3, '0');
           
       return vRes;

    end ms_to_char;
   
   procedure parada_de(de number) is
      CSEGUNDO constant number:= 1000;
      vFec date:= sysdate;
      vIni number:= milisegundos;
      vFin number;
      vMax number:= de * CSEGUNDO;
      vTot number;
   begin
      vFecIni:= vFec;
      
      loop
         vFin:= milisegundos;
         vTot:= vFin - vIni;
         exit when vTot > vMax;
      end loop;
      
      vFecFin:= sysdate;
      
   end parada_de;
   
begin
   vInicio:= milisegundos;
   parada_de(2);
   vFinal:= milisegundos;
   dbms_output.put_line(  'Inicia en '      || to_char(vFecIni,'hh24:mi:ss') || ' (' || vInicio
                       || '), finaliza en ' || to_char(vFecFin,'hh24:mi:ss') || ' (' || vFinal
                       || '), duró '        || ms_to_char(vFinal - vInicio)
                      );
end;
-1

you can use DBMS_SESSION.SLEEP which replaces DBMS_LOCK.SLEEP in Oracle Database 18c https://oracle-base.com/articles/18c/dbms_session-sleep-18c#dbms_session

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.