Sub topic 10
Row Locks and What Other Sessions See
Assume you know basic SELECT, UPDATE, COMMIT, and ROLLBACK. This chapter also assumes you can open two database sessions, such as two SQL Developer worksheets or two SQL*Plus connections. Both sessions must use the same Oracle database and schema.
When a session changes a row, Oracle places a row lock on that row. The lock protects the uncommitted change. Oracle holds the lock until the transaction ends with COMMIT or ROLLBACK.
A writer locks changed rows
Suppose the accounts table contains account 2:
SELECT id, owner, balance
FROM accounts
WHERE id = 2;In Session A, update the row without committing:
UPDATE accounts
SET balance = balance + 10
WHERE id = 2;Session A owns the row lock. Session B cannot make a conflicting change to the same row until Session A ends its transaction:
-- Session B
UPDATE accounts
SET balance = balance - 5
WHERE id = 2;This statement waits. It does not immediately fail because Oracle expects Session A might commit soon.
If Session A commits, Session B continues and applies its update to the committed result:
-- Session A
COMMIT;If Session A rolls back instead, Session B continues after the original value is restored:
-- Session A
ROLLBACK;A session that changes a different row usually does not wait. Oracle locks rows, not the entire table, although some operations can acquire broader locks.
Readers usually do not wait
A normal query does not wait for another session's uncommitted row change. It uses read consistency, which means Oracle reconstructs the last committed version of the data from undo information.
-- Session A
UPDATE accounts
SET balance = balance + 10
WHERE id = 2;
-- Session B
SELECT balance
FROM accounts
WHERE id = 2;Session B sees the balance that was committed before Session A's update. Session B does not see Session A's uncommitted value.
The query gets a consistent view for the statement. If another session commits a change while a long query is running, that query continues using its statement-level view. A later query can see the newly committed value.
Locking rows for later work
Use SELECT FOR UPDATE when a session must read rows and then update those same rows safely. The query locks the selected rows until the transaction ends.
SELECT id, balance
FROM accounts
WHERE id = 2
FOR UPDATE;
UPDATE accounts
SET balance = balance - 25
WHERE id = 2;
COMMIT;FOR UPDATE can wait if another session already owns a conflicting lock. Add NOWAIT to fail immediately, or SKIP LOCKED to ignore locked rows:
SELECT id, balance
FROM accounts
WHERE balance > 0
FOR UPDATE SKIP LOCKED;SKIP LOCKED is useful when several workers claim separate jobs. It is not appropriate when every requested row must be processed.
What the isolation level changes
Oracle's default isolation level is READ COMMITTED. Each statement sees data committed before that statement began. A transaction can therefore see different committed values in two separate queries.
SERIALIZABLE gives a transaction a consistent view from its start. If another transaction changes data that would conflict with the serializable transaction, Oracle can raise ORA-08177: cannot serialize access.
READ ONLY permits queries but rejects changes in the transaction.
Practical rules
- Keep transactions short so locks are released quickly.
- Commit or roll back every unit of business work.
- Do not leave a session idle after changing rows.
- Expect conflicting writers to wait.
- Use
NOWAITwhen waiting is unacceptable. - Investigate deadlocks rather than solving them with frequent commits. A deadlock occurs when sessions wait for locks held by one another, and Oracle aborts one statement.
Exercise
Session A updates account 2 and does not commit. What does a normal SELECT in Session B see, and what happens when Session B updates account 2?
Show answer
The SELECT sees the last committed value. The UPDATE waits for Session A to commit or roll back, unless the statement uses a nonwaiting option such as NOWAIT.