Sub topic 9
Read Consistency and Isolation Levels in Oracle
Assume you know basic SELECT, INSERT, UPDATE, and COMMIT statements, and can connect to Oracle with SQL Developer, SQLcl, or SQL*Plus. This chapter focuses on what a session sees while other sessions change the same data.
Oracle's read-consistency model
Read consistency means Oracle gives a query a logically consistent view of data. A query does not normally see another session's uncommitted changes. Instead, Oracle uses undo data to reconstruct the last committed version of a row when necessary.
Oracle's default isolation level is READ COMMITTED. In this mode, each statement gets its own read-consistent view. Two queries in the same transaction can therefore return different results if another session commits changes between them.
A query that starts before another session commits continues using a consistent view for that query. It does not switch halfway through the result set to newly committed values.
Isolation levels
An isolation level controls how a transaction sees changes made by other transactions.
| Isolation level | Consistency scope | Common result |
|---|---|---|
| `READ COMMITTED` | Each statement | Later statements can see newly committed data |
| `SERIALIZABLE` | The transaction's starting point | Later queries see a stable snapshot |
| `READ ONLY` | The transaction's starting point | Queries are allowed, but changes are rejected |
Oracle does not provide a traditional dirty-read mode. A dirty read would expose data before its transaction commits. Oracle avoids this even under READ COMMITTED.
READ COMMITTED
This is the normal default. It provides statement-level consistency and allows good concurrency. However, a transaction that performs two separate queries can observe a changed value between them.
SERIALIZABLE
Use SERIALIZABLE when a transaction must behave as though other transactions committed either before it began or after it ended. The transaction can issue changes, but Oracle may reject a write if the target data changed after the transaction began. The error is commonly ORA-08177: can't serialize access for this transaction.
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT balance
FROM accounts
WHERE id = 1;
-- Perform related reads and writes here.
-- A conflicting concurrent change can cause ORA-08177.
COMMIT;SET TRANSACTION must be the first statement in the transaction. End the transaction with COMMIT or ROLLBACK before selecting a different isolation level.
READ ONLY
A read-only transaction gives all its queries a consistent view from the transaction's start. Attempts to change data fail.
SET TRANSACTION READ ONLY;
SELECT owner, balance
FROM accounts
ORDER BY id;
-- INSERT, UPDATE, and DELETE statements are not allowed here.
COMMIT;A read-only transaction is useful for a report that must use one stable view, although long-running reports can require substantial undo retention.
Choosing an isolation level
Use READ COMMITTED for most short business transactions. Use SERIALIZABLE when the application needs transaction-wide consistency and can retry serialization failures. Use READ ONLY for consistent reports that must not modify data. Keep transactions short, because long-running transactions retain older data versions and can increase pressure on undo space.
Exercise
Session A starts a READ COMMITTED transaction and runs a query. Session B then commits an update. What can Session A's next query see?
Show answer
The next query can see Session B's newly committed value because READ COMMITTED provides a separate consistent view for each statement.