Sub topic 2
ACID and How Oracle Delivers It
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and can connect to Oracle with SQL*Plus, SQL Developer, or SQLcl. This chapter explains the four ACID properties that make an Oracle transaction reliable.
ACID means:
| Property | Meaning | Oracle mechanism |
|---|---|---|
| Atomicity | All related changes succeed or none do | Transaction rollback and statement-level atomicity |
| Consistency | Data obeys defined rules before and after a transaction | Constraints, data types, triggers, and valid SQL |
| Isolation | Concurrent transactions do not see unsafe intermediate results | Read consistency and row-level locking |
| Durability | Committed changes survive a failure | Redo generation and recovery |
Atomicity: all or nothing
A transaction groups statements into one unit of work. Consider a transfer of 100 from Alice to Bob:
CREATE TABLE accounts (
id NUMBER PRIMARY KEY,
owner VARCHAR2(50) NOT NULL,
balance NUMBER(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO accounts (id, owner, balance) VALUES (1, 'Alice', 1000);
INSERT INTO accounts (id, owner, balance) VALUES (2, 'Bob', 200);
COMMIT;
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
COMMIT;The two updates represent one business action. If an error occurs before the final COMMIT, use ROLLBACK to discard both updates. Oracle does not leave half of the transfer committed when you roll back the transaction.
Atomicity also applies to an individual SQL statement. If one UPDATE affects many rows and a constraint failure prevents the statement from completing, Oracle rolls back that statement's effects. Earlier successful statements in the same transaction remain available unless you explicitly roll back the transaction.
Consistency: rules remain true
Consistency means a successful transaction moves the database from one valid state to another valid state. Oracle helps enforce this through:
- Primary keys, which identify rows uniquely.
- Foreign keys, which protect relationships between tables.
NOT NULL,CHECK, and data type rules.- Triggers and application validation where appropriate.
In the example, CHECK (balance >= 0) prevents an update from creating a negative balance. A failed update does not prove that the whole transaction failed. You must decide whether to correct the problem, roll back to a savepoint, or roll back the entire transaction.
Isolation: concurrent work stays safe
Isolation controls what one session can observe while another session changes data. Oracle's default READ COMMITTED behavior gives each statement a consistent view of committed data. A query does not normally read another session's uncommitted update.
At the same time, Oracle locks rows changed by an open transaction. A second session trying to update the same row waits until the first session commits or rolls back. This prevents two writers from silently overwriting the same row.
Durability: committed means recoverable
When Oracle processes COMMIT, it records the transaction's change information in redo. Redo is used during instance recovery to reapply committed changes after a crash. Undo supports rollback and consistent reads, while redo supports recovery of committed work.
Durability does not mean every later failure is harmless. A committed transaction can still be affected by media loss or an improperly configured recovery strategy. Database backup and recovery procedures remain necessary.
Exercise
A transfer debits Alice successfully, but the credit to Bob fails before COMMIT. Which ACID properties are involved, and what should the session do?
Show answer
Atomicity requires both changes to succeed or neither to remain. The session should correct the problem or issue ROLLBACK so the debit is discarded.