Overview

Oracle’s concurrency model rests on multi-version read consistency: readers use undo to see a snapshot and never block writers or get blocked by them (oracle-redo-undo). Writers lock only the rows they change; there is no lock escalation and no shared read locks. Compare sqlserver-locking-blocking-deadlocks and locking-and-mvcc.

Core concepts

  • Isolation levels: READ COMMITTED (default; statement-level consistency), SERIALIZABLE (transaction-level snapshot; ORA-08177 can’t serialize) and READ ONLY. No dirty reads, ever. See isolation-levels.
  • Row locks live in the data block (an interested transaction list, ITL) rather than in a lock table; a waiting session queues on the TX enqueue (enq: TX - row lock contention). TM locks protect the table definition from DDL while DML runs (mode 3, row exclusive).
  • Foreign keys without an index cause table-level locking on the child when the parent key changes; index foreign keys (oracle-indexes).
  • Deadlock: two sessions wait on each other; Oracle detects it, rolls back only the statement that was waiting (ORA-00060: deadlock detected) and writes a trace file. The transaction stays open; the application should roll back or retry.
  • Other tools: SELECT ... FOR UPDATE [NOWAIT | WAIT n | SKIP LOCKED], DBMS_LOCK, latches and mutexes (internal memory serialisation).

Worked example

Executed on the Free container with two sessions on lk(id, bal):

Session A: UPDATE lk SET bal = bal - 10 WHERE id = 1; and keeps the transaction open. Then

SessionStatementResult
B (reader)SELECT bal FROM lk WHERE id = 1returned 100 after 0 s, the committed value from undo
C (writer)UPDATE lk SET bal = bal + 1 WHERE id = 1waited 9 s until A rolled back

What the dictionary showed while C waited:

SELECT sid, blocking_session, event, seconds_in_wait FROM v$session WHERE blocking_session IS NOT NULL;
-- 59 | 220 | enq: TX - row lock contention | 3
SELECT s.sid, l.type, l.lmode, l.request, l.block FROM v$lock l JOIN v$session s ON s.sid = l.sid ...;
-- 220: TM mode 3, TX mode 6, block 1       59: TM mode 3, TX request 6

Deadlock test: A updates row 1 then row 2; B updates row 2 then row 1, four seconds apart. A received ORA-00060: deadlock detected while waiting for resource for its second statement; B completed. The transaction of A was not rolled back by Oracle; its first update was still pending until it ended the transaction.

Pitfalls and production notes

  • Long uncommitted transactions hold row locks and undo; monitor v$transaction and the oldest start_time.
  • ORA-00054: resource busy and acquire with NOWAIT indicates DDL against a table with active DML; schedule DDL or use online operations.
  • Application code must handle ORA-00060 and ORA-08177; retry with backoff.
  • Queue-style work: SELECT ... FOR UPDATE SKIP LOCKED or Advanced Queuing.
  • Lost updates under READ COMMITTED are still possible in read-modify-write code; use FOR UPDATE, update in SQL (bal = bal - 10) or optimistic version columns.

Practice questions

  1. Why does Oracle have no lock escalation?
  2. What happens to a transaction after ORA-00060?
  3. How do you find who blocks a session using V$SESSION?

References

  • Oracle Database Concepts, Data Concurrency and Consistency

oracle-redo-undo · isolation-levels · sqlserver-locking-blocking-deadlocks · pg-mvcc