2081 1

BIT352 ยท 2081 ยท 10 marks

Describe levels of locking in oracle. Discuss the methods used to acquire locks in oracle.

Describe levels of locking in oracle. Discuss the methods used to acquire locks in oracle.[10]

Database AdministrationChapter 8: Monitoring, Tuning and PerformanceLocking levels in oracle

Quick answer

A lock is a mechanism that prevents destructive interaction between transactions accessing a shared resource. Oracle Database uses a multi-level locking strategy to provide maximum concurrency while maintaining data integrity and consistency.

Levels of Locking in Oracle and Methods to Acquire Locks

Note: The reference notes did not contain specific content on this topic. The following answer is based on standard Oracle database concepts as taught in database management system courses at the BSc CSIT level.


Introduction to Locking in Oracle

Oracle's locking philosophy is:

  • Locks are acquired automatically (implicitly) by DML statements.
  • Oracle never escalates locks (a key difference from many other RDBMS).
  • Readers do not block writers, and writers do not block readers.

Levels of Locking in Oracle

Oracle implements locking at multiple granularity levels:


1. Row-Level Locks (TX Locks - Transaction Locks)

  • The finest granularity of locking in Oracle.
  • When a transaction modifies a row (INSERT, UPDATE, DELETE, SELECT FOR UPDATE), Oracle places an exclusive lock on that specific row.
  • Other transactions can read the row (using MVCC - Multi-Version Concurrency Control) but cannot modify it until the lock is released.
  • Row-level locks are stored within the data block itself (in the row header), not in a separate lock table.
  • This allows Oracle to lock millions of rows without lock escalation.

Example:

UPDATE employees SET salary = 5000 WHERE emp_id = 101;
-- Only row with emp_id = 101 is locked

2. Table-Level Locks (TM Locks - DML Locks)

  • Placed on the entire table to protect the table structure while row-level locks are held.
  • Table locks do not prevent other transactions from accessing rows; they prevent DDL operations (like DROP TABLE or ALTER TABLE) from conflicting with ongoing DML.
  • Oracle supports five modes of table-level locks:
ModeAbbreviationDescription
Row ShareRS (SS)Allows concurrent access; prevents exclusive table lock
Row ExclusiveRX (SX)Used during DML; prevents share and exclusive locks
ShareSAllows only reads; prevents DML by others
Share Row ExclusiveSRX (SSX)More restrictive than Share; only one transaction can hold it
ExclusiveXFull exclusive access; no other DML or DDL allowed

Lock Compatibility Matrix:

RSRXSSRXX
RSYYYYN
RXYYNNN
SYNYNN
SRXYNNNN
XNNNNN

(Y = Compatible, N = Incompatible)


3. Page/Block-Level Locks

  • Oracle does not use page-level or block-level locking as a standard locking granularity.
  • This is a deliberate design choice to avoid unnecessary contention.
  • Lock information is embedded in the block header (Interested Transaction List - ITL), but this is not a separate locking level.

4. Database-Level Locks (DDL Locks)

  • Protect the definition (schema) of database objects during DDL operations.
  • Three types:
    • Exclusive DDL Lock: Acquired during DROP, ALTER, CREATE operations. No other DDL or DML allowed on the object.
    • Share DDL Lock: Acquired during CREATE PROCEDURE, CREATE VIEW. Allows other share DDL locks but not exclusive.
    • Breakable Parse Lock: Held by SQL statements in the shared pool. Can be broken if a DDL operation requires it.

5. System/Dictionary Locks (Internal Locks and Latches)

  • Latches: Low-level serialization mechanisms protecting internal Oracle memory structures (e.g., SGA, buffer cache). They are not user-visible.
  • Internal Locks: Protect internal database structures like data file headers and control files.
  • These are managed entirely by Oracle and are not accessible to users.

Methods Used to Acquire Locks in Oracle

Oracle provides both implicit (automatic) and explicit (manual) methods to acquire locks.


Method 1: Implicit (Automatic) Locking

Oracle automatically acquires locks when DML statements are executed. The user does not need to request them.

SQL StatementRow LockTable Lock Mode
INSERTExclusive (X) on new rowRX
UPDATEExclusive (X) on modified rowsRX
DELETEExclusive (X) on deleted rowsRX
SELECTNo lockNo lock (MVCC used)

Example:

UPDATE departments SET dept_name = 'HR' WHERE dept_id = 10;
-- Oracle automatically acquires: Row X lock + Table RX lock

Locks are automatically released when the transaction ends (COMMIT or ROLLBACK).


Method 2: SELECT ... FOR UPDATE (Explicit Row Lock)

  • Used to explicitly lock rows that a transaction intends to modify later.
  • Prevents other transactions from modifying or locking the same rows.
  • Useful when a transaction reads data and then updates it later in the same transaction.

Syntax:

SELECT column_list
FROM table_name
WHERE condition
FOR UPDATE [OF column] [NOWAIT | WAIT n | SKIP LOCKED];

Options:

  • NOWAIT: Returns an error immediately if the lock cannot be acquired.
  • WAIT n: Waits for n seconds before returning an error.
  • SKIP LOCKED: Skips rows that are already locked.

Example:

SELECT emp_id, salary
FROM employees
WHERE dept_id = 10
FOR UPDATE NOWAIT;
-- The selected rows are locked in exclusive mode until COMMIT or ROLLBACK

Method 3: LOCK TABLE (Explicit Table Lock)

  • Used when a transaction wants to lock an entire table rather than individual rows.
  • The lock mode is stated explicitly, which lets the developer decide how much concurrency to give up.

Syntax:

LOCK TABLE table_name IN lock_mode MODE [NOWAIT];

Common lock modes:

ModeMeaning
ROW SHARE (RS)Allows concurrent access, only prevents exclusive table locks
ROW EXCLUSIVE (RX)Acquired automatically by INSERT, UPDATE and DELETE
SHARE (S)Allows other readers, blocks all writers
SHARE ROW EXCLUSIVE (SRX)Allows readers only, one transaction at a time
EXCLUSIVE (X)Strictest, only queries are allowed on the table

Example:

LOCK TABLE employees IN EXCLUSIVE MODE NOWAIT;

Method 4: DDL Locks (Acquired Automatically)

  • Oracle takes a DDL lock on an object whenever a DDL statement such as ALTER TABLE or DROP TABLE refers to it, so the definition cannot change under a statement that is still compiling or executing.
  • These locks are never requested by the user; they are held only for the duration of the DDL statement.

Conclusion

Oracle locks at several granularities, from the row level (TX locks, the normal case) up through table level (TM locks), block level, and the DDL and internal locks that protect the data dictionary. Locks are acquired in two ways: implicitly, when a DML statement runs and Oracle takes the row and table locks it needs, and explicitly, through SELECT ... FOR UPDATE for rows or LOCK TABLE for whole tables. Oracle's default is deliberately conservative in favour of concurrency, since readers never block writers and writers never block readers thanks to multiversion read consistency, and every lock is released automatically at COMMIT or ROLLBACK.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.