DBMS_LOCK.sleep procedure to suspend the session in Oracle
DBMS_LOCK.sleep procedure suspends the session for a given period of time or specified time in the procedure, it will wait to complete procedure then next SQL statement will execute.
DBMS_LOCK.SLEEP (seconds IN NUMBER);
seconds: Amount of time, in seconds, to suspend the session.
The smallest increment can be entered in hundredths of a second; for example, 1.95 is a legal time value.
Below is the step by step demonstration on DBMS_LOCK.sleep. We will complete it three phase:
In the first phase, we will create a user and try to execute DBMS_LOCK.SLEEP using newly created user.
create user dbsguru identified by dbsguru;
grant create session to dbsguru;
conn dbsguru/dbsguru
show user
EXEC DBMS_LOCK.sleep(60);
It got failed due to insufficient privileges.
In the second phase, we will grant access to user DBSGURU and try to execute DBMS_LOCK.SLEEP again.
By default, DBMS_LOCK is not accessible by normal users so grant is required for a specific session/schema to use this package.
grant execute on sys.dbms_lock to dbsguru;
conn dbsguru/dbsguru
show user
EXEC DBMS_LOCK.sleep(60);
Third phase, in this final phase, we will execute EXEC DBMS_LOCK.sleep package along with other SQL statements where we will see its actually taking to complete (holding session for seconds as the specified value in package) 1 minute and then moving to next SQL statement and so on.
We will create one small .sql file as below to validate
vi DBMS_LOCK_sleep.sql
set timing on
set echo on
show user
EXEC DBMS_LOCK.sleep(60);
select username from user_users;
EXEC DBMS_LOCK.sleep(60);
select count(1) from v$session;
As we can see above procedure is taking to complete one minute of time as specified value i.e the procedure suspends the session for the specified number of seconds. to execute the next SQL statement.
Note: 1. EXEC DBMS_LOCK.sleep is deprecated from 18c but still available for backward compatibility.
2. We can use procedure DBMS_SESSION.SLEEP in 18c where it’s the limit maximum value in seconds 3600 and does not require additional privileges for normal users.
This document is only for learning purpose and always validate in the LAB environment first before applying in the LIVE environment.
Hope so you like this article!
Please share your valuable feedback/comments/subscribe and follow us below and don’t forget to click on the bell icon to get the latest update. Click here to know more about our pursuit.
Related Articles
- How to Perform a Health Check of an Oracle Database
- Step by Step How to increase SGA size in Oracle
- How to run sql tuning advisor in Oracle
- How to Check Retention Period of AWR Snapshot in Oracle
- How to Find Last Analyzed date on Tables, Partition, and Subpartition in Oracle
Nice option..when we fetch report from the database from app server.. but I doubt it will create holding session n in db it will appear as blocking session which will misguide the dba. So on general basis dba will kill tht holding session from dba sessions.. it is useful when load is low on database. Which lock it will use that have to check.
Thanks & appreciate Vishal for your concern!! No it will not hold any session in term of blocking sessions since here using package DBMS_LOCK.sleep and same has been tested. So its safe to use according to require and appreciate if you also validate same in lab / test environment and let us know if any concerns on same.
Thanks.
Team DbsGuru