Sub topic 2
ACID and how Oracle delivers it
ACID is the set of four guarantees a database makes about transactions. This chapter maps each one to the Oracle feature that provides it, so you know what the database does for you and what you still have to do yourself.
It assumes you have read the previous chapter on what a transaction is. You should be able to run SQL in SQL*Plus, SQL Developer, or SQLcl, and the accounts table from the setup script should exist with Alice at 1000 and Bob at 200. The undo, redo, and isolation topics are covered in depth in later chapters, so this chapter only outlines them.
The four properties
ACID stands for Atomicity, Consistency, Isolation, and Durability. Each letter answers a different question about what happens when work is interrupted or runs at the same time as other work.
| Property | Question it answers | Main Oracle mechanism | What you must do |
|---|---|---|---|
| Atomicity | Can half a transaction be saved? | Undo data, statement-level rollback | Keep one business unit of work in one transaction |
| Consistency | Can the data break the rules? | Constraints, triggers | Declare the rules as constraints |
| Isolation | Do concurrent sessions see each other's half-finished work? | Multiversion read consistency, row locks | Pick an isolation level that fits the work |
| Durability | Is a committed change lost in a crash? | Redo log written at commit | Wait for COMMIT to return before telling anyone it is saved |
Atomicity: all or nothing
A transaction is atomic if either all of its changes become permanent or none do. In the money transfer from the setup, the debit of Alice and the credit of Bob must stand or fall together.
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;Until COMMIT, Oracle keeps the information needed to reverse both updates in undo data. ROLLBACK uses that undo data to restore the old values. If the session crashes before the commit, Oracle rolls the transaction back during recovery, and nobody ever sees a state with the money removed from Alice but not added to Bob.
Oracle also applies atomicity to each single statement. If one statement fails halfway, Oracle undoes everything that statement did, and the transaction stays open with its earlier work intact. Try a statement that changes two rows, where the second change breaks the CHECK (balance >= 0) rule:
UPDATE accounts SET balance = balance - 300 WHERE id IN (1, 2);
-- ORA-02290: check constraint (...) violated
SELECT id, balance FROM accounts WHERE id IN (1, 2) ORDER BY id;
-- id 1: 1000 id 2: 200 (Alice's row was not left at 700)Alice's row would have been valid at 700, but Bob's would have gone to -100. The statement fails as a unit, so neither row changes. A later chapter covers this behavior in detail.
Consistency: valid state to valid state
Consistency means a transaction moves the database from one valid state to another. This is the property people most often misunderstand. Oracle does not know your business rules. It enforces the rules you declare: primary keys, unique keys, foreign keys, NOT NULL, and CHECK constraints. A rule you only keep in application code is a rule a different program, or a manual UPDATE, can break.
In the accounts table, CHECK (balance >= 0) is what stops an overdraft. Without it, the transfer transaction would be perfectly atomic and durable and would still leave a negative balance.
Immediate and deferred checking
By default, Oracle checks a constraint at the end of each statement. A constraint declared DEFERRABLE INITIALLY DEFERRED is checked only at COMMIT. That lets a transaction pass through a temporarily invalid state, as long as the final state is valid.
CREATE TABLE ledger_entries (
id NUMBER PRIMARY KEY,
amount NUMBER NOT NULL,
CONSTRAINT ck_amount_positive CHECK (amount > 0)
DEFERRABLE INITIALLY DEFERRED
);
INSERT INTO ledger_entries (id, amount) VALUES (1, -5); -- succeeds for now
UPDATE ledger_entries SET amount = 5 WHERE id = 1; -- repairs the row
COMMIT; -- succeedsIf you skip the repair, the COMMIT fails:
INSERT INTO ledger_entries (id, amount) VALUES (2, -5);
COMMIT;
-- ORA-02091: transaction rolled back
-- ORA-02290: check constraint (...CK_AMOUNT_POSITIVE) violatedBecause the CREATE TABLE above is DDL, it also commits any transaction that was open when you ran it. The next chapters explain that rule.
Isolation: concurrent sessions do not interfere
Isolation controls what one transaction can see of another transaction's work. Oracle uses multiversion read consistency. When a query runs, Oracle shows it the data as it was committed at one point in time. If a row has been changed by a transaction that has not committed, the query rebuilds the old version from undo data.
The consequences for everyday work are:
- A session never sees another session's uncommitted changes. Oracle does not allow dirty reads.
- Readers do not wait for writers, and writers do not wait for readers.
- Two sessions that change the same row do conflict. The second one waits for the first to commit or roll back.
The default level is READ COMMITTED: each statement sees the data committed when that statement began. You can raise it for a whole transaction:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
-- ... statements that must see one consistent snapshot of the data ...
COMMIT;SET TRANSACTION must be the first statement of the transaction. A serializable transaction can fail with ORA-08177: can't serialize access for this transaction when another session changed a row it tries to change, and your code must be ready to retry. The isolation chapter compares the three options (READ COMMITTED, SERIALIZABLE, READ ONLY) in detail.
Durability: committed means safe
Durability means that once COMMIT returns successfully, the change survives a crash of the instance or the server. Oracle achieves this with the redo log. Every change generates redo records describing it. When you commit, the log writer process (LGWR) writes your transaction's redo to the online redo log files on disk, and only then does COMMIT return to you.
The changed data blocks themselves may reach the data files much later. That is safe, because after a crash, instance recovery replays the redo to restore committed changes (roll forward) and uses undo to remove the work of transactions that never committed (roll back).
How the properties work together
The two update statements of a transfer touch all four properties at once:
- Atomicity: both updates are tracked in undo as one transaction, so they commit or roll back together.
- Consistency: the
CHECKconstraint rejects any statement that would leave a negative balance. - Isolation: other sessions keep reading the old balances until you commit, and a competing update of the same row waits.
- Durability:
COMMITreturns only after the redo for both updates is on disk.
Common errors
- Assuming Oracle enforces business rules it has not been told about. If an invoice total must equal the sum of its lines, a constraint or trigger must say so, or the application must check it.
- Splitting one business action across several transactions. If a client commits after the debit and again after the credit, a crash between the two leaves the data inconsistent and atomicity cannot help.
- Autocommit. Many drivers, JDBC for example, commit after every statement by default. Each statement then becomes its own transaction, and the debit and credit are no longer atomic. Turn autocommit off and commit explicitly.
- Running DDL in the middle of a transaction. DDL commits the open transaction first, so earlier work becomes permanent and can no longer be rolled back.
- Opening a second connection and expecting to see uncommitted work. A second session is a different transaction and, by isolation, sees only committed data.
- Treating a retry as optional under
SERIALIZABLE.ORA-08177is a normal outcome and the transaction should be rerun.
Exercise
A client program has autocommit turned on and runs the two UPDATE statements of a transfer (debit Alice by 100, credit Bob by 100). The program crashes after the first UPDATE. Which ACID property is effectively lost, and what is the fix?
Show answer
Atomicity is lost. With autocommit on, the debit committed as its own transaction, so Alice lost 100 while Bob received nothing. The fix is to turn autocommit off, run both UPDATE statements in one transaction, and issue a single COMMIT after the second one. Then a crash before the COMMIT causes Oracle to roll back both changes.
Exercise
You run UPDATE accounts SET balance = balance - 300 WHERE id IN (1, 2); and it fails with ORA-02290 because Bob's balance would go negative. You had already updated another row earlier in the same transaction and have not committed. After the error, what is the state of Alice's balance, and of the earlier change?
Show answer
Alice's balance is unchanged at 1000, because Oracle rolls back the entire failed statement, not only the row that violated the constraint. The earlier change is still in place and the transaction is still open, because statement-level atomicity undoes only the failed statement. You can still COMMIT to keep the earlier change or ROLLBACK to discard it.