Sub topic 3
Read consistency and isolation levels
This chapter assumes you know basic SQL (SELECT, INSERT, UPDATE, DELETE), can connect to an Oracle database, and can read simple PL/SQL blocks. It covers how Oracle shows data to concurrent sessions, how row locks make writers wait, and how to set transaction boundaries and handle errors.
Read consistency and isolation levels
Oracle gives each query a consistent view of the data as of the moment the query started. It builds that view from undo data, so readers never wait for writers. The default level is READ COMMITTED, where each statement sees only committed data.
-- Session A
UPDATE accounts SET balance = balance + 10 WHERE id = 2; -- not committed yet
-- Session B
SELECT balance FROM accounts WHERE id = 2; -- returns the old committed value immediatelyUse SET TRANSACTION before any other statement in the transaction when you need a stronger view:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;makes every query in the transaction see one snapshot taken at the start.SET TRANSACTION READ ONLY;is for reports that must read a stable snapshot without changing data.
Row locks and what other sessions see
An UPDATE, DELETE, or INSERT locks the rows it changes until the transaction ends. A second session that changes the same row waits. Readers never wait.
sequenceDiagram
participant A as Session A
participant DB as Oracle
participant B as Session B
A->>DB: UPDATE row 2 (locks row)
B->>DB: SELECT row 2
DB-->>B: Old committed value
B->>DB: UPDATE row 2
Note over B,DB: Session B waits
A->>DB: COMMIT
DB-->>B: Update proceedscaption: Session B reads without waiting but must wait for Session A to commit before it can update the same row.
alt: Sequence diagram. Session A updates row 2 and locks it. Session B reads the old value at once, then tries to update row 2 and waits. Session A commits, and then Session B's update proceeds.
Transaction control in PL/SQL
PL/SQL blocks can use COMMIT and ROLLBACK, and SAVEPOINT. Handle errors per row so that one bad row does not undo the whole batch.
SET SERVEROUTPUT ON
BEGIN
FOR r IN (SELECT id FROM accounts) LOOP
SAVEPOINT before_row;
BEGIN
UPDATE accounts SET balance = balance - 100 WHERE id = r.id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT before_row;
DBMS_OUTPUT.PUT_LINE('Skipped ' || r.id || ': ' || SQLERRM);
END;
END LOOP;
COMMIT;
END;
/Autonomous transactions for logging and auditing
An autonomous transaction commits on its own, so a log row survives even if the caller rolls back. Declare it with PRAGMA AUTONOMOUS_TRANSACTION and always COMMIT inside it.
CREATE TABLE audit_log (
id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
msg VARCHAR2(200),
logged_at TIMESTAMP DEFAULT SYSTIMESTAMP
);
CREATE OR REPLACE PROCEDURE log_msg(p_msg IN VARCHAR2) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO audit_log (msg) VALUES (p_msg);
COMMIT;
END log_msg;
/COMMIT WRITE options for redo durability trade-offs
COMMIT WRITE controls how redo is written. The default is IMMEDIATE WAIT, which is the safest. COMMIT WRITE BATCH NOWAIT; returns faster but can lose the most recent commits if the instance fails. Use the faster options only for data you can rebuild.
Designing transaction boundaries and avoiding pitfalls
Make one transaction match one business unit of work, such as a full transfer. Keep it short, because open row locks block other users. Do not put COMMIT in the middle of a unit of work that must succeed or fail together. Remember that DDL such as CREATE TABLE commits any open work first.
Distributed transactions and two-phase commit (overview)
When one transaction changes data in two databases, Oracle uses two-phase commit. In the prepare phase, each database confirms it can commit. In the commit phase, every database commits, or all roll back. This protects consistency across databases but adds network waits and can leave locks held if a link fails.
Exercise
In READ COMMITTED, Session A updates row 2 without committing. Does Session B's SELECT of row 2 wait, and what value does it see?
Show answer
It does not wait. Session B sees the last committed value of row 2, not Session A's uncommitted change.