Sub topic 12
Row Locks and Concurrent Sessions
Prerequisites and the working model
This chapter assumes that you can write basic SELECT, INSERT, UPDATE, and DELETE statements. You should also understand COMMIT, ROLLBACK, primary keys, and foreign keys.
A concurrent session is another SQL connection working with the database at the same time. Oracle lets many sessions read and change data, but it must protect rows that one session is currently changing.
When a session changes a row, Oracle places a row lock on that row. The lock normally remains until the session commits or rolls back. Other sessions can work with different rows in the same table, but a session that tries to change the locked row must wait.
What happens when two sessions update one row
Assume that session A and session B are connected to the same database.
In session A, run:
UPDATE emp_demo
SET salary = salary + 100
WHERE emp_id = 1;Do not commit yet. The changed value is visible to session A, but it is not yet committed.
Now session B runs:
UPDATE emp_demo
SET salary = salary + 200
WHERE emp_id = 1;Session B waits. It cannot change the same row until session A ends its transaction.
If session A commits, session B continues and applies its update to the row's current value. If session A rolls back, session B updates the value that existed before session A's change.
A normal query usually does not wait for an uncommitted row change. Oracle uses read consistency: the query sees a consistent version of the data from before the conflicting change. This lets readers and writers work at the same time.
Locking rows intentionally
Use SELECT ... FOR UPDATE when a transaction must read rows and then change those same rows. Oracle locks the selected rows until the transaction ends.
SELECT emp_id, salary
FROM emp_demo
WHERE dept_id = 20
FOR UPDATE;This is useful when an application needs to inspect a value, make a decision, and update it safely. A second session that selects the same rows with FOR UPDATE waits.
You can choose a different response:
SELECT emp_id, salary
FROM emp_demo
WHERE dept_id = 20
FOR UPDATE NOWAIT;NOWAIT returns an error immediately if another session holds a required lock. SKIP LOCKED skips locked rows instead, which is useful for multiple workers processing a queue.
SELECT emp_id
FROM emp_stage
WHERE status = 'READY'
FOR UPDATE SKIP LOCKED;Blocking, timeouts, and deadlocks
Blocking is expected when two sessions need the same row. The safest solution is usually to keep transactions short and commit promptly after the intended work is complete. Do not leave a transaction open while waiting for user input.
A deadlock occurs when two sessions wait for each other. For example, session A locks row 1 and then requests row 2, while session B locks row 2 and then requests row 1. Oracle detects the cycle and raises ORA-00060: deadlock detected while waiting for resource in one session.
Applications should update rows in a consistent order and retry the transaction when appropriate.
Exercise
Session A updates employee 1 and does not commit. Session B runs a normal SELECT for employee 1, then tries to update that employee. Which operation waits?
Show answer
The normal SELECT usually returns a read-consistent value. The UPDATE waits until session A commits or rolls back.