Sub topic 2
Implicit commits: DDL, normal exit, and abnormal termination
You should know basic SQL (SELECT, INSERT, UPDATE, DELETE), be able to connect to Oracle with SQL*Plus, SQL Developer, or SQLcl, and understand the accounts table from the earlier chapter.
An implicit commit is a commit that Oracle performs without a COMMIT statement from you. It makes all pending changes in the current transaction permanent.
DDL statements
Data definition language (DDL) changes database structure. Examples are CREATE, ALTER, DROP, and TRUNCATE. Oracle commits the open transaction before and after each DDL statement.
INSERT INTO accounts (id, owner, balance) VALUES (5, 'Eve', 300);
CREATE TABLE scratch_table (x NUMBER); -- commits Eve's row first
ROLLBACK; -- nothing left to undo
SELECT owner FROM accounts WHERE id = 5; -- returns 'Eve'Normal exit
SQL*Plus commits pending work when you leave with EXIT or QUIT. SQL Developer asks whether to commit or roll back before it disconnects, so check the prompt.
Abnormal termination
If the session is killed, the client crashes, or the network drops, Oracle does not commit. The background process PMON rolls back the orphaned transaction, so uncommitted work is lost.
Why a failed statement does not abort the whole transaction
When one statement fails, Oracle undoes only that statement. Earlier statements remain pending, and the transaction stays open. You can fix the problem, retry, or roll back the whole transaction yourself.
Partial rollback with savepoints
A savepoint marks a point inside a transaction. ROLLBACK TO SAVEPOINT undoes the work done after that point and keeps the transaction open.
SAVEPOINT before_debit;
UPDATE accounts SET balance = balance - 50 WHERE id = 1;
ROLLBACK TO SAVEPOINT before_debit; -- the UPDATE is undone
COMMIT;What happens inside: undo and redo
Before Oracle changes a row, it stores the old values in undo data. ROLLBACK uses that undo data to restore them. Oracle also records every change in the redo log. COMMIT returns only after the redo is written to disk, so committed changes survive a crash. Other sessions read committed values by using undo data to reconstruct them.
Exercise
You insert a row, run CREATE TABLE, and then run ROLLBACK. Is the inserted row still in the table?
Show answer
Yes. The CREATE TABLE statement committed the insert before it ran, so the later ROLLBACK has nothing to undo.