The SLEEP procedure blocks the current session for the specified duration.
Applicability
This topic applies only to OceanBase Database Enterprise Edition. OceanBase Database Community Edition does not support this feature.
Syntax
DBMS_LOCK.SLEEP (
seconds IN NUMBER);
Parameters
Parameter |
Description |
|---|---|
| seconds | The duration for which the session is blocked, in seconds. You can specify the value in increments of 0.01 seconds. For example, 1.95 is a valid value. |
Considerations
secondsaccepts decimals with a precision of up to 0.01 seconds. Internally, the sleep is implemented in microseconds. Passing a negative number will cause an error.During the sleep period, the session continuously checks its own status. If the session is killed or a command times out, the sleep will be interrupted with an error and the session will not remain occupied indefinitely.
SLEEPis commonly used for delayed execution, simulating slow business operations, and testing lock waits and timeout scenarios. Do not useSLEEPin production code to implement polling, as prolonged sleep will occupy the session connection.
Examples
Block the current session for 1.5 seconds and verify the actual duration using DBMS_UTILITY.GET_TIME.
obclient> SET SERVEROUTPUT ON;
obclient> DECLARE
v_start NUMBER;
v_end NUMBER;
BEGIN
v_start := DBMS_UTILITY.GET_TIME;
DBMS_LOCK.SLEEP(1.5);
v_end := DBMS_UTILITY.GET_TIME;
DBMS_OUTPUT.PUT_LINE('Elapsed: ' || (v_end - v_start) * 0.01 || ' seconds');
END;
/
The return result is as follows:
Elapsed: 1.54 seconds