Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

About User Locks

Note: The return set for a SELECT... FOR UPDATE may change while the query is running; for example, if columns selected by the query are updated or rows are deleted after the query started.

When this happens, SELECT... FOR UPDATE acquires locks on the rows that did not change, gets a new read-consistent snapshot of the table using these locks, and then restarts the query to acquire the remaining locks.

This can cause a deadlock between sessions querying the table concurrently with DML operations when rows are locked in a non-sequential order. To prevent such deadlocks, design your application so that any concurrent DML on the table does not affect the return set of the query. If this is not feasible, you may want to serialize queries in your application.

When acquiring row locks with SELECT... FOR UPDATE, you can specify the NOWAIT option to indicate that you are not willing to wait to acquire the lock. If you cannot acquire then lock immediately, an error is returned to signal that the lock is not possible at this time. You can try to lock the row again later.

By default, the transaction waits until the requested row lock is acquired. If the wait for a row lock is too long, you can code logic into your application to cancel the lock operation and try again later.

About User Locks

You can use Oracle Lock Management services for your applications by making calls to the DBMS_LOCK package. It is possible to request a lock of a specific mode, give it a unique name recognizable in another procedure in the same or another instance, change the lock mode, and release it. Because a reserved user lock is the same as an Oracle Database lock, it has all the features of a database lock, such as deadlock detection. Be certain that any user locks used in distributed transactions are released upon COMMIT, or an undetected deadlock can occur.

See Also: PL/SQL Packages and Types Reference for detailed information on the DBMS_LOCK package

How Oracle Database Processes SQL Statements 5-17

About User Locks

When to Use User Locks

User locks can help to:

Provide exclusive access to a device, such as a terminal

Provide application-level enforcement of read locks

Detect when a lock is released and cleanup after the application

Synchronize applications and enforce sequential processing

Example of a User Lock

The following 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.

*****************************************************************

* Print Check *

*Any cashier may issue a refund to a customer returning goods. *

*Refunds under $50 are given in cash, more than $50 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.

*

*****************************************************************

CHECK-PRINT

*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).

EXEC SQL EXECUTE

BEGIN DBMS_LOCK.REQUEST ( :LOCKHANDLE );

END; END-EXEC.

*We now have exclusive use of the printer, print the check.

...

*Unlock the printer so other people can use it

EXEC SQL EXECUTE

BEGIN DBMS_LOCK.RELEASE ( :LOCKHANDLE );

END; END-EXEC.

5-18 Oracle Database Application Developer's Guide - Fundamentals

Соседние файлы в папке Oracle 10g