|Oracle9i Supplied PL/SQL Packages and Types Reference
Release 1 (9.0.1)
Part Number A89852-02
DBMS_LOCK , 6 of 6
This procedure suspends the session for a given period of time.
The smallest increment can be entered in hundredths of a second; for example, 1.95 is a legal time value.
This Pro*COBOL precompiler example shows how locks can be used to ensure that there are no conflicts when multiple people need to access a single device.
Any cashier may issue a refund to a customer returning goods. Refunds under $50 are given in cash; anything above that is given by check. This code prints the check. The one printer is opened by all the cashiers to avoid the overhead of opening and closing it for every check. This means that lines of output from multiple cashiers could become interleaved if we don't ensure exclusive access to the printer. The
DBMS_LOCK package is used to ensure exclusive access.
Get the lock "handle" for the printer lock:
MOVE "CHECKPRINT" TO LOCKNAME-ARR. MOVE 10 TO LOCKNAME-LEN. EXEC SQL EXECUTE BEGIN DBMS_LOCK.ALLOCATE_UNIQUE ( :LOCKNAME, :LOCKHANDLE ); END; END-EXEC.
Lock the printer in exclusive mode (default mode):
We now have exclusive use of the printer, print the check:
Unlock the printer so other people can use it: