transaction level | Transactions including the following SQL statements override Oracle's default locking: the LOCK TABLE command, the SELECT command including the FOR UPDATE clause, and the SET TRANSACTION command with the READ ONLY or ISOLATION LEVEL SERIALIZABLE options. Locks acquired by these statements are released after the transaction is committed or rolled back. |
system level | An instance can be started with non-default locking by adjusting the initialization parameters SERIALIZABLE and ROW LOCKING. |
Warning: If you override the default locking of Oracle at any level, be sure that the overriding locking procedures operate correctly; that is, be sure that data integrity is guaranteed, data concurrency is acceptable, and deadlocks are not possible or are appropriately handled.
LOCK TABLE emp, dept IN EXCLUSIVE MODE NOWAIT;
You can specify several tables or views to lock in the same mode; however, only a single lock mode can be specified per LOCK TABLE statement.
Note: When a table is locked, all rows of the table are locked. No other user can modify the table.
You can also indicate if you do or do not want to wait to acquire the lock. If you specify the NOWAIT option, you only acquire the table lock if it is immediately available. Otherwise an error is returned to notify that the lock is not available at this time. In this case, you can attempt to lock the resource at a later time. If NOWAIT is omitted, the transaction does not proceed until the requested table lock is acquired. If the wait for a table lock is excessive, you might want to cancel the lock operation and retry at a later time; you can code this logic into your applications.
Note: A distributed transaction waiting for a table lock can timeout waiting for the requested lock if the elapsed amount of time reaches the interval set by the initialization parameter DISTRIBUTED_LOCK_TIMEOUT. Because no data has been modified, no actions are necessary as a result of the time-out. Your application should proceed as if a deadlock has been encountered. For more information on distributed transactions, refer to Oracle7 Server Distributed Systems, Volume I.
The following paragraphs provide guidance on when it can be advantageous to acquire each type of table lock using the LOCK TABLE command.
ROW SHARE and ROW EXCLUSIVE
LOCK TABLE table IN ROW SHARE MODE;
LOCK TABLE table IN ROW EXCLUSIVE MODE;
Row share and row exclusive table locks offer the highest degree of concurrency. Conditions that possibly warrant the explicit acquisition of a row share or row exclusive table lock include the following:
LOCK TABLE table IN SHARE MODE;
Share table locks are rather restrictive data locks. The following conditions could warrant the explicit acquisition of a share table lock:
For example, assume that two tables, EMP and BUDGET, require a consistent set of data in a third table, DEPT. That is, for a given department number, you want to update the information in both of these tables, and ensure that no new members are added to the department between these two transactions.
Although this scenario is quite rare, it can be accommodated by locking the DEPT table in SHARE MODE, as shown in the following example. Because the DEPT table is not highly volatile, few, if any, users would need to update it while it was locked for the updates to EMP and BUDGET.
LOCK TABLE dept IN SHARE MODE UPDATE EMP SET sal = sal * 1.1 WHERE deptno IN (SELECT deptno FROM dept WHERE loc = 'DALLAS') UPDATE budget SET totsal = totsal * 1.1 WHERE deptno IN (SELECT deptno FROM dept WHERE loc = 'DALLAS') COMMIT /* This releases the lock */
SHARE ROW EXCLUSIVE
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE;
Conditions that warrant the explicit acquisition of a share row exclusive table lock include the following:
LOCK TABLE table IN EXCLUSIVE MODE;
Conditions that warrant the explicit acquisition of an exclusive table lock include the following: