The combination of multiversion concurrency control and row-level locking means that users only contend for data when accessing the same rows, specifically:
For a complete description of the internal locks used by Oracle, see "Types of Locks" .
Oracle releases all locks acquired by the statements within a transaction when you either commit or roll back the transaction. Oracle also releases locks acquired after a savepoint when rolling back to the savepoint. However, only transactions not waiting on the previously locked resources can acquire locks on the now available resources. Waiting transactions will continue to wait until after the original transaction commits or rolls back completely.
Oracle automatically converts a table lock of lower restrictiveness to one of higher restrictiveness as appropriate. For example, assume that a transaction uses a SELECT statement with the FOR UPDATE clause to lock rows of a table. As a result, it acquires the exclusive row locks and a row share table lock for the table. If the transaction later updates one or more of the locked rows, the row share table lock is automatically converted to a row exclusive table lock. For more information about table locks, see "Table Locks" .
Oracle does not escalate any locks at any time from one level of granularity (for example, rows) to another (for example, table), reducing the chance of deadlocks.
Note: In distributed transactions, local deadlocks are detected by analyzing a "waits for" graph, and global deadlocks are detected by a time-out. Once detected, non-distributed and distributed deadlocks are handled by the database and application in the same way.
Deadlocks most often occur when transactions explicitly override the default locking of Oracle. Because Oracle itself does no lock escalation and does not use read locks for queries, but does use row-level locking (rather than page-level locking), deadlocks occur infrequently in Oracle. See "Explicit (Manual) Data Locking" for more information about manually acquiring locks and for an example of a deadlock situation.
When you know you will require a sequence of locks for one transaction, you should consider acquiring the most exclusive (least compatible) lock first.
Later sections also describe situations where you might wish to acquire locks manually or to alter the default locking behavior of Oracle and explain how you can do so; see "Explicit (Manual) Data Locking" .
Throughout its operation, Oracle automatically acquires different types of locks at different levels of restrictiveness depending on the resource being locked and the operation being performed. Oracle locks fall into one of the following general categories:
data locks (DML locks) | Data locks protect data. For example, table locks lock entire tables, row locks lock selected rows. |
dictionary locks (DDL locks) | Dictionary locks protect the structure of objects. For example, dictionary locks protect the definitions of tables and views. |
internal locks and latches Internal locks and latches protect internal database structures such as datafiles. Internal locks and latches are entirely automatic. | |
distributed locks | Distributed locks ensure that the data and other resources distributed among the various instances of an Oracle Parallel Server remain consistent. Distributed locks are held by instances rather than transactions. They communicate the current status of a resource among the instances of an Oracle Parallel Server. |
parallel cache management (PCM) locks | Parallel cache management locks are distributed locks that cover one or more data blocks (table or index blocks) in the buffer cache. PCM locks do not lock any rows on behalf of transactions. |
DML operations can acquire data locks at two different levels: for specific rows and for entire tables. The following sections explain row and table locks.
A transaction acquires an exclusive data lock for each individual row modified by one of the following statements: INSERT, UPDATE, DELETE, and SELECT with the FOR UPDATE clause.
A modified row is always locked exclusively so that other users cannot modify the row until the transaction holding the lock is committed or rolled back. Row locks are always acquired automatically by Oracle as a result of the statements listed above.
Rows Locks and Table Locks If a transaction obtains a row lock for a row, the transaction also acquires a table lock for the corresponding table. A table lock also must be obtained to prevent conflicting DDL operations that would override data changes in a current transaction. The following section explains table locks, and "DDL Locks (Dictionary Locks)" explains the locks necessary for DDL operations.
A table lock can be held in any of several modes: row share (RS), row exclusive (RX), share lock (S), share row exclusive (SRX), and exclusive (X). The restrictiveness of a table lock's mode determines the modes in which other table locks on the same table can be obtained and held.
Table 10 - 1 shows the modes of table locks that statements acquire and operations that those locks permit and prohibit.
The following sections explain each mode of table lock, from least restrictive to most restrictive. Each section describes the mode of table lock, the actions that cause the transaction to acquire a table lock in that mode, and which actions are permitted and prohibited in other transactions by a lock in that mode. For more information about manual locking, see "Explicit (Manual) Data Locking" .
Row Share Table Locks (RS) A row share table lock (also sometimes internally called a sub-share table lock, SS) indicates that the transaction holding the lock on the table has locked rows in the table and intends to update them. A row share table lock is automatically acquired for a table when one of the following SQL statements is executed:
SELECT . . . FROM table . . . FOR UPDATE OF . . . ;
LOCK TABLE table IN ROW SHARE MODE;
A row share table lock is the least restrictive mode of table lock, offering the highest degree of concurrency for a table.
Permitted Operations: A row share table lock held by a transaction allows other transactions to query, insert, update, delete, or lock rows concurrently in the same table. Therefore, other transactions can obtain simultaneous row share, row exclusive, share, and share row exclusive table locks for the same table.
Prohibited Operations: A row share table lock held by a transaction prevents other transactions from exclusive write access to the same table using only the following statement:
LOCK TABLE table IN EXCLUSIVE MODE;
Row Exclusive Table Locks (RX) A row exclusive table lock (also internally called a sub-exclusive table lock, SX) generally indicates that the transaction holding the lock has made one or more updates to rows in the table. A row exclusive table lock is acquired automatically for a table modified by the following types of statements:
INSERT INTO table . . . ;
UPDATE table . . . ;
DELETE FROM table . . . ;
LOCK TABLE table IN ROW EXCLUSIVE MODE;
A row exclusive table lock is slightly more restrictive than a row share table lock.
Permitted Operations: A row exclusive table lock held by a transaction allows other transactions to query, insert, update, delete, or lock rows concurrently in the same table. Therefore, row exclusive table locks allow multiple transactions to obtain simultaneous row exclusive and row share table locks for the same table.
Prohibited Operations: A row exclusive table lock held by a transaction prevents other transactions from manually locking the table for exclusive reading or writing. Therefore, other transactions cannot concurrently lock the table using the following statements:
LOCK TABLE table IN SHARE MODE;
LOCK TABLE table IN SHARE EXCLUSIVE MODE;
LOCK TABLE table IN EXCLUSIVE MODE;
Share Table Locks (S) A share table lock is acquired automatically for the table specified in the following statement:
LOCK TABLE table IN SHARE MODE;
Permitted Operations: A share table lock held by a transaction allows other transactions only to query the table, to lock specific rows with SELECT . . . FOR UPDATE, or to execute LOCK TABLE . . . IN SHARE MODE statements successfully; no updates are allowed by other transactions. Multiple transactions can hold share table locks for the same table concurrently. In this case, no transaction can update the table (even if a transaction holds row locks as the result of a SELECT statement with the FOR UPDATE clause). Therefore, a transaction that has a share table lock can only update the table if no other transactions also have a share table lock on the same table.
Prohibited Operations: A share table lock held by a transaction prevents other transactions from modifying the same table and from executing the following statements:
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE;
LOCK TABLE table IN EXCLUSIVE MODE;
LOCK TABLE table IN ROW EXCLUSIVE MODE;
Share Row Exclusive Table Locks (SRX) A share row exclusive table lock (also sometimes called a share-sub-exclusive table lock, SSX) is more restrictive than a share table lock. A share row exclusive table lock is acquired for a table as follows:
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE;
Permitted Operations: Only one transaction at a time can acquire a share row exclusive table lock on a given table. A share row exclusive table lock held by a transaction allows other transactions to query or lock specific rows using SELECT with the FOR UPDATE clause, but not to update the table.
Prohibited Operations: A share row exclusive table lock held by a transaction prevents other transactions from obtaining row exclusive table locks and modifying the same table. A share row exclusive table lock also prohibits other transactions from obtaining share, share row exclusive, and exclusive table locks, which prevents other transactions from executing the following statements:
LOCK TABLE table IN SHARE MODE;
LOCK TABLE table IN SHARE ROW EXCLUSIVE MODE;
LOCK TABLE table IN ROW EXCLUSIVE MODE;
LOCK TABLE table IN EXCLUSIVE MODE;
Exclusive Table Locks (X) An exclusive table lock is the most restrictive mode of table lock, allowing the transaction that holds the lock exclusive write access to the table. An exclusive table lock is acquired for a table as follows:
LOCK TABLE table IN EXCLUSIVE MODE;
Permitted Operations: Only one transaction can obtain an exclusive table lock for a table. An exclusive table lock permits other transactions only to query the table.
Prohibited Operations: An exclusive table lock held by a transaction prohibits other transactions from performing any type of DML statement or placing any type of lock on the table.
DML Statement | Row Locks? | Mode of Table Lock |
SELECT ...FROM table | ||
INSERT INTO table ... | _/ | RX |
UPDATE table ... | _/ | RX |
DELETE FROM table ... | _/ | RX |
SELECT ... FROM table ... FOR UPDATE OF ... | _/ | RS |
LOCK TABLE table IN ... | ||
ROW SHARE MODE | RS | |
ROW EXCLUSIVE MODE | RX | |
SHARE MODE | S | |
SHARE EXCLUSIVE MODE | SRX | |
EXCLUSIVE MODE | X | |
SELECT
INSERT . . . SELECT . . . ;
UPDATE . . . ;
DELETE . . . ;
They do not include the following statements:
SELECT . . . FOR UPDATE OF . . . ;
Note that INSERT, UPDATE, and DELETE statements can have implicit queries as part of the statement.
Queries are the SQL statements least likely to interfere with other SQL statements because they only read data. The following characteristics are true of all queries that do not use the FOR UPDATE clause:
A dictionary lock is acquired automatically by Oracle on behalf of any DDL transaction requiring it. Users cannot explicitly request DDL locks. Only individual schema objects that are modified or referenced are locked during DDL operations; the whole data dictionary is never locked.
DDL locks fall into three categories: exclusive DDL locks, share DDL locks, and breakable parse locks.
In addition to DDL locks, DDL operations also acquire DML locks (data locks) on the object to be modified.
Most DDL operations acquire exclusive DDL locks on the object to be modified (except for those listed in the next section, "Share DDL Locks").
During the acquisition of an exclusive DDL lock, if another DDL lock is already held on the object by another operation, the acquisition waits until the older DDL lock is released and then proceeds.
A share DDL lock is acquired on an object for DDL statements on the object that include the following commands: AUDIT, NOAUDIT, COMMENT, CREATE [OR REPLACE] VIEW/ PROCEDURE/PACKAGE/PACKAGE BODY/FUNCTION/ TRIGGER, CREATE SYNONYM, and CREATE TABLE (when the CLUSTER parameter is not included).
A parse lock is acquired during the parse phase of SQL statement execution and held as long as the shared SQL area remains in the shared pool.
Internal locks are higher-level, more complex mechanisms than latches and serve a variety of purposes. Details on the three categories of internal locks follow.
Dictionary Cache Locks These locks are of very short duration and are held on entries in dictionary caches while the entries are being modified or used. They guarantee that statements being parsed do not see inconsistent object definitions.
Dictionary cache locks can be shared or exclusive. Shared locks are released when the parse is complete. Exclusive locks are released when the DDL operation is complete.
File and Log Management Locks These locks protect different files. For example, one lock protects the control file so that only one process at a time can change it. Another lock coordinates the use and archiving of the redo log files. Datafiles are locked to ensure that multiple instances mount a database in shared mode or that one instance mounts it in exclusive mode. Because file and log locks indicate the status of files, these locks are necessarily held for a long time.
File and log locks are of particular importance if you are using the Oracle Parallel Server. For more information. see Oracle7 Parallel Server Concepts & Administration.
Tablespace and Rollback Segment Locks These locks protect tablespaces and rollback segments. For example, all instances accessing a database must agree on whether a tablespace is online or offline. Rollback segments are locked so that only one instance can write to a segment.
transaction level | Transactions including the following SQL statements override Oracle's default locking: the LOCK TABLE command (which locks either a table or, when used with views, the underlying base tables) and the SELECT.. FOR UPDATE command. Locks acquired by these statements are released after the transaction commits or rolls back. For information about each command, see the Oracle7 Server SQL Reference . |
session level | A session can set the required transaction isolation level with the ALTER SESSION command. |
system level | An instance can be started with non-default locking by adjusting the initialization parameter ISOLATION_LEVEL. |
Note: For brevity, the message text for ORA-00054 is not included, but reads "resource busy and acquire with NOWAIT specified." User-entered text is in bold.
Transaction 1 | Time Point | Transaction 2 |
LOCK TABLE scott.dept IN ROW SHARE MODE; Statement processed
| 1 | DROP TABLE scott.dept; DROP TABLE scott.dept * ORA-00054 (exclusive DDL lock not possible because of T1's table lock) LOCK TABLE scott.dept IN EXCLUSIVE MODE NOWAIT; ORA-00054
|
2 | ||
3 | ||
4 | SELECT LOC FROM scott.dept WHERE deptno = 20 FOR UPDATE OF loc; LOC - - - - - - - DALLAS 1 row selected | |
UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 20; (waits because T2 has locked same rows)
| 5 |
|
6 | ROLLBACK; (releases row locks) | |
1 row processed. ROLLBACK;
| 7 | |
LOCK TABLE scott.dept IN ROW EXCLUSIVE MODE; Statement processed.
| 8 | |
9 |
LOCK TABLE scott.dept IN EXCLUSIVE MODE NOWAIT; ORA-00054
| |
10 |
LOCK TABLE scott.dept IN SHARE ROW EXCLUSIVE MODE NOWAIT; ORA-00054
| |
11 | LOCK TABLE scott.dept IN SHARE ROW EXCLUSIVE MODE NOWAIT; ORA-00054 | |
12 |
UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 20; 1 row processed.
| |
13 |
ROLLBACK;
| |
SELECT loc FROM scott.dept WHERE deptno = 20 FOR UPDATE OF loc; LOC - - - - - - DALLAS 1 row selected.,
| 14 | |
15 |
UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 20; (waits because T1 has locked same rows)
| |
ROLLBACK; | 16 | |
17 | 1 row processed. (conflicting locks were released) ROLLBACK; | |
LOCK TABLE scott.dept IN ROW SHARE MODE Statement processed | 18 | |
19 |
LOCK TABLE scott.dept IN EXCLUSIVE MODE NOWAIT; ORA-00054
| |
20 | LOCK TABLE scott.dept IN SHARE ROW EXCLUSIVE MODE NOWAIT; ORA-00054 | |
21 |
LOCK TABLE scott.dept IN SHARE MODE; Statement processed.
| |
22 |
SELECT loc FROM scott.dept WHERE deptno = 20; LOC - - - - - - DALLAS 1 row selected.
| |
23 |
SELECT loc FROM scott.dept WHERE deptno = 20 FOR UPDATE OF loc; LOC - - - - - - DALLAS 1 row selected.
| |
24 |
UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 20; (waits because T1 holds conflicting table lock)
| |
ROLLBACK;
| 25 | |
26 |
1 row processed. (conflicting table lock released) ROLLBACK;
| |
LOCK TABLE scott.dept IN SHARE ROW EXCLUSIVE MODE; Statement processed.
| 27 | |
28 |
LOCK TABLE scott.dept IN EXCLUSIVE MODE NOWAIT; ORA-00054
| |
29 | LOCK TABLE scott.dept IN SHARE ROW EXCLUSIVE MODE NOWAIT; ORA-00054 | |
30 | LOCK TABLE scott.dept IN SHARE MODE NOWAIT; ORA-00054 | |
31 |
LOCK TABLE scott.dept IN ROW EXCLUSIVE MODE NOWAIT; ORA-00054
| |
32 | LOCK TABLE scott.dept IN SHARE MODE NOWAIT; ORA-00054 | |
33 | SELECT loc FROM scott.dept WHERE deptno = 20; LOC - - - - - - DALLAS 1 row selected. | |
34 | SELECT loc FROM scott.dept WHERE deptno = 20 FOR UPDATE OF loc; LOC - - - - - - DALLAS 1 row selected. | |
35 | UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 20; (waits because T1 holds conflicting table lock) | |
UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 20; (waits because T2 has locked same rows) | 36 | (deadlock) |
Cancel operation ROLLBACK;
| 37 | |
38 |
1 row processed
| |
LOCK TABLE scott.dept IN EXCLUSIVE MODE; | 39 | |
40 | LOCK TABLE scott.dept IN EXCLUSIVE MODE; ORA-00054 | |
41 | LOCK TABLE scott.dept IN ROW EXCLUSIVE MODE NOWAIT; ORA-00054 | |
42 | LOCK TABLE scott.dept IN SHARE MODE; ORA-00054 | |
43 | LOCK TABLE scott.dept IN ROW EXCLUSIVE MODE NOWAIT; ORA-00054 | |
44 | LOCK TABLE scott.dept IN ROW SHARE MODE NOWAIT; ORA-00054 | |
45 | SELECT loc FROM scott.dept WHERE deptno = 20; LOC - - - - - - DALLAS 1 row selected. | |
46 |
SELECT loc FROM scott.dept WHERE deptno = 20 FOR UPDATE OF loc; (waits because T1 has conflicting table lock)
| |
UPDATE scott.dept SET deptno = 30 WHERE deptno = 20; 1 row processed.
| 47 | |
COMMIT;
| 48 | |
49 |
0 rows selected. (T1 released conflicting lock)
| |
SET TRANSACTION READ ONLY;
| 50 | |
SELECT loc FROM scott.dept WHERE deptno = 10; LOC - - - - - - BOSTON
| 51 | |
52 |
UPDATE scott.dept SET loc = 'NEW YORK' WHERE deptno = 10; 1 row processed.
| |
SELECT loc FROM scott.dept WHERE deptno = 10; LOC - - - - - - (T1 does not see uncommitted data)
| 53 | |
54 | COMMIT; | |
SELECT loc FROM scott.dept WHERE deptno = 10; LOC - - - - - - (same results seen even after T2 commits) | 55 | |
COMMIT; | 56 | |
SELECT loc FROM scott.dept WHERE deptno = 10; LOC - - - - - - NEW YORK (committed data is seen) | 57 | |
The Oracle Lock Management services are available through procedures in the DBMS_LOCK package. For more information about Oracle Lock Management services, see the Oracle7 Server Application Developer's Guide.