Question: What
is the difference between row locks and table locks in
Oracle?
Will Oracle ever escalate a row-level lock to a table
lock?
Answer: First, note these
Oracle lock scripts and Oracle
deadlocks.
Because Oracle row locks are managed internally,
there is no limit on row-level locking and it is never
necessary to perform "lock escalation" as you might see in
lesser database systems. There is never any lock
escalation to the block level or table level; it is always
row-level locking.
Also, Oracle never locks rows
except for DML operations (insert, update, delete).
A
writer of data does not block a reader of data, and a writer
of data is blocked only when another writer of data has
already locked the target row. (a deadlock)
This
constant row-level locking means that you do not have to do
frequent commits to release Oracle locks. You should
resist using COMMIT statements unless required by the
application and allow long-running DML transactions to run
to completion without frequent COMMIT to make them
restartable.
Row Locks (TX)
Row-level locks serve a primary function to prevent multiple
transactions from modifying the same row. Whenever a
transaction needs to modify a row, a row lock is acquired by
Oracle.
There is no hard limit on the exact number
of row locks held by a statement or transaction. Also,
unlike other database platforms, Oracle will never escalate
a lock from the row level to a coarser granular level. This
row locking ability provides the DBA with the finest
granular level of locking possible and, as such, provides
the best possible data concurrency and performance for
transactions.
The mixing of multiple concurrency
levels of control and row level locking means that users
face contention for data only whenever the same rows are
accessed at the same time. Furthermore, readers of
data will never have to wait for writers of the same data
rows. Writers of data are not required to wait for readers
of these same data rows except in the case of when a
SELECT... FOR UPDATE is used.
Writers will only wait
on other writers if they try to update the same rows at the
same point in time. In a few special cases, readers of data
may need to wait for writers of the same data. For example,
concerning certain unique issues with pending transactions
in distributed database environments with Oracle.
Transactions will acquire exclusive row locks for individual
rows that are using modified INSERT, UPDATE, and DELETE
statements and also for the SELECT with the FOR UPDATE
clause.
Modified rows are always locked in exclusive
mode with Oracle so that other transactions do not modify
the row until the transaction which holds the lock issues a
commit or is rolled back. In the event that the Oracle
database transaction does fail to complete successfully due
to an instance failure, then Oracle database block level
recovery will make a row available before the entire
transaction is recovered. The Oracle database provides the
mechanism by which row locks acquire automatically for the
DML statements mentioned above.
Whenever a
transaction obtains row locks for a row, it also acquires a
table lock for the corresponding table. Table locks prevent
conflicts with DDL operations that would cause an override
of data changes in the current transaction.
Table Locks (TM)
What are table locks in
Oracle? Table locks perform concurrency control for
simultaneous DDL operations so that a table is not dropped
in the middle of a DML operation, for example. When Oracle
issues a DDL or DML statement on a table, a table lock is
then acquired. As a rule, table locks do not affect
concurrency of DML operations. Locks can be acquired at both
the table and sub-partition level with partitioned tables in
Oracle.
A transaction acquires a table lock when a
table is modified in the following DML statements: INSERT,
UPDATE, DELETE, SELECT with the FOR UPDATE clause, and LOCK
TABLE. These DML operations require table locks for two
purposes: to reserve DML access to the table on behalf of a
transaction and to prevent DDL operations that would
conflict with the transaction.
Any table lock
prevents the acquisition of an exclusive DDL lock on the
same table, and thereby prevents DDL operations that require
such locks. For example, a table cannot be altered or
dropped if an uncommitted transaction holds a table lock for
it.
A table lock can be held in any of several
modes: row share (RS), row exclusive (RX), share (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.
|
|
|
Oracle Training from Don Burleson
The best on site
"Oracle
training classes" are just a phone call away! You can get personalized Oracle training by Donald Burleson, right at your shop!
|
|
|
|
|
Burleson is the American Team
Note:
This Oracle
documentation was created as a support and Oracle training reference for use by our
DBA performance tuning consulting professionals.
Feel free to ask questions on our
Oracle forum.
Verify
experience!
Anyone
considering using the services of an Oracle support expert should
independently investigate their credentials and experience, and not rely on
advertisements and self-proclaimed expertise. All legitimate Oracle experts
publish
their Oracle
qualifications.
Errata?
Oracle technology is changing and we
strive to update our BC Oracle support information. If you find an error
or have a suggestion for improving our content, we would appreciate your
feedback. Just
e-mail:
and include the URL for the page.
Copyright © 1996 - 2020
All rights reserved by
Burleson
Oracle ®
is the registered trademark of Oracle Corporation.
|
|