Question:
How does row level locking work
in Oracle?
Answer: When a transaction begins it opens
an exclusive lock on a resource. The name of the lock depends
on the transaction ID for the freshly started transaction.
This resource then, if requested by another transaction, will serve
as a wait point until the transaction holding the lock performs
either a commit or a rollback.
For example, assume we have two transactions, each requesting the
same resource. The first transaction accesses the resource and
generates a lock. The second transaction will detect that the
resource it is trying to access is locked by the first transaction,
and would then request a lock in exclusive mode, on the resource.
When the first transaction commits or performs a rollback, it will
release the exclusive lock on the resource. When the lock is
released, the second transaction gains an exclusive lock and
proceeds.
This is a step by step look at the row locking mechanism:
- Transaction begins either implicitly via any DML, or
explicitly via set transaction.
- A slot is allocated in the rollback segment header.
This slot is allocated either via a round robin selection
process, or specified by the set transaction use rollback
segment ... statement.
- Blocks to be changed are identifired using the execution
plan, utilizing whatever access paths are specified there.
- The transaction looks for an empty slot on the Interested
Transaction List (ITL). If there are none available, one
will be allocated from free space in the block if it is
available. If that fails, the transaction will wait on a
TX enqueue in mode 4. When the ITL slot has been
allocated, it will point to the rollback segment header that was
previously reserved by the transaction.
- When the ITL slot is allocated in the block, specific rows
are marked as locked in the row directory. The lock byte
will be set to point to the ITL slot held by the transaction.
This is the mechanism that acutally maintains the row-level
locks in Oracle. Once the lock byte is set, the row can be
operated on. If changes to the block are made, the before
images are recorded in the rollback segment reserved by the
transaction.
- When a commit or rollback is performed, the lock is
released. When a commit occurs, in order to release the
lock, the rollback segment header slot must be marked as
committed. Cleanup of the block itself is usually delayed
until a later time.
|
|
Get the Complete
Oracle SQL Tuning Information
The landmark book
"Advanced Oracle
SQL Tuning The Definitive Reference" is
filled with valuable information on Oracle SQL Tuning.
This book includes scripts and tools to hypercharge Oracle 11g
performance and you can
buy it
for 30% off directly from the publisher.
|