Sub topic 4
Undoing changes with ROLLBACK
ROLLBACK discards every change your session has made in the current transaction and ends that transaction. This chapter covers what it undoes, what it leaves alone, and how it behaves when a statement fails partway through.
It assumes you can run SELECT, INSERT, UPDATE, and DELETE in SQL*Plus, SQL Developer, or SQLcl. It also assumes you know that a transaction is a group of statements that succeed or fail together. The examples use the accounts table from the setup chapter, with Alice at 1000 and Bob at 200.
What ROLLBACK does
A transaction begins with your first DML statement after the previous COMMIT or ROLLBACK, or after connecting. ROLLBACK does four things:
- It reverses every uncommitted change made by the transaction, in the reverse order they were made.
- It releases the row locks the transaction holds.
- It ends the transaction, so the next DML statement starts a new one.
- It leaves committed data untouched.
The syntax is ROLLBACK;. The keyword WORK is optional and means the same thing: ROLLBACK WORK;.
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
SELECT id, balance FROM accounts WHERE id IN (1, 2) ORDER BY id;
-- Your own session sees 900 and 300
ROLLBACK;
SELECT id, balance FROM accounts WHERE id IN (1, 2) ORDER BY id;
-- Back to 1000 and 200Your own session sees its uncommitted changes. Other sessions never saw them. After the rollback, nobody sees them.
Calling ROLLBACK with no open transaction is harmless. It does nothing and raises no error. That makes it safe to put at the start of a script that must begin from a clean state.
How it works, briefly
Before Oracle changes a row, it stores the old version in undo data. ROLLBACK applies that undo data to put the old values back. The undo and redo chapter covers this in detail. Two consequences matter now:
- Rollback does real work. A transaction that updated millions of rows takes noticeable time to roll back, often about as long as the original work or longer.
- Rolling back is not free just because nothing was committed. Other sessions that need a locked row wait until the rollback finishes.
What ROLLBACK does not undo
Rollback covers DML changes made inside the transaction. It does not cover everything your session did.
| Item | Rolled back? | Why |
|---|---|---|
| INSERT, UPDATE, DELETE, MERGE | Yes | They are recorded in undo. |
| Changes made by triggers fired by your DML | Yes | They belong to the same transaction. |
| Committed changes | No | They are permanent. |
| DDL such as `CREATE TABLE` or `TRUNCATE` | No | DDL commits implicitly, so there is nothing left to undo. |
| Sequence values from `NEXTVAL` | No | Sequences are not transactional. |
| Work done in an autonomous transaction | No | It commits independently. |
| PL/SQL variables and session settings | No | They are not database data. |
The sequence behavior surprises many people:
CREATE SEQUENCE demo_seq;
SELECT demo_seq.NEXTVAL FROM dual; -- returns 1
ROLLBACK;
SELECT demo_seq.NEXTVAL FROM dual; -- returns 2, not 1The same applies to identity columns, which use sequences internally. If you roll back an insert into a table with an identity column, that number is skipped. Do not rely on gap-free numbering from sequences or identity columns.
The DDL row is the other common trap, because it can leave you believing a change is still undoable:
INSERT INTO accounts (id, owner, balance) VALUES (4, 'Dan', 10);
CREATE TABLE scratch_table (x NUMBER); -- commits the INSERT first
ROLLBACK; -- nothing left to undo
SELECT owner FROM accounts WHERE id = 4; -- still returns 'Dan'A failed statement rolls back only itself
When a single statement fails, Oracle undoes that statement's partial effects and leaves the rest of your transaction open. The failure does not roll back earlier statements.
UPDATE accounts SET balance = balance + 100 WHERE id = 2; -- succeeds, still uncommitted
UPDATE accounts SET balance = balance - 5000 WHERE id = 1;
-- ORA-02290: check constraint (...) violated
-- Alice's row is unchanged, but Bob's +100 is still pending
SELECT balance FROM accounts WHERE id = 2; -- 300: the first update is still there
ROLLBACK; -- discards Bob's +100This is statement-level atomicity. A multi-row statement is also all or nothing: if an UPDATE touches 500 rows and fails on row 400, the first 399 changes are undone, and the statement leaves no partial result.
The consequence for your code is that an error does not by itself roll anything back. After a failure, your code must choose: fix the problem and continue, or issue ROLLBACK. If an application ignores the error and then runs COMMIT, it commits the earlier successful statements and leaves out the failed one. In a money transfer, that means money leaves one account and never arrives in the other.
Rolling back part of a transaction
A plain ROLLBACK is all or nothing for the whole transaction. To undo only the recent part, set a savepoint earlier and use ROLLBACK TO SAVEPOINT name. That form keeps the transaction open and keeps the locks acquired before the savepoint. The next chapter covers it in full. For now, remember that ROLLBACK without TO SAVEPOINT always ends the transaction.
ROLLBACK in PL/SQL
ROLLBACK works the same way inside a PL/SQL block. It undoes the work of the whole transaction, not only the work done in that block. A transaction does not start or end at BEGIN and END.
BEGIN
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
UPDATE accounts SET balance = balance + 100 WHERE id = 2;
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END;
/The handler rolls back and then re-raises the error with RAISE, so the caller still learns that the transfer failed. If you roll back and swallow the exception, the caller assumes success. If a PL/SQL block raises an unhandled exception, Oracle rolls back only the statement-level work of the block that failed, and earlier committed or pending work in the session remains your responsibility.
Rollback you did not ask for
Oracle also rolls back automatically in these situations:
- If your session ends abnormally, for example a network drop or a killed process, Oracle rolls back the uncommitted transaction. Normal exit from SQL*Plus commits instead, which the implicit commit chapter explains.
- If the instance crashes, the next startup rolls back every uncommitted transaction as part of recovery.
- If the database detects a deadlock between two sessions, it rolls back one statement in one session. That session's transaction is still open, so your code should usually roll back and retry.
In-doubt distributed transactions can be resolved by a DBA with ROLLBACK FORCE. That is outside this chapter.
Common mistakes
- Running DDL in the middle of a unit of work and then expecting
ROLLBACKto undo earlier DML. DDL commits first. - Treating an error as a rollback. The failed statement is undone, but earlier statements are not.
- Expecting sequence numbers to be reused after a rollback.
- Leaving a transaction open after an error. Its row locks keep other sessions waiting until you roll back or commit.
- Rolling back inside a handler and then not re-raising the error, which hides the failure from the caller.
Exercise
You run INSERT INTO accounts (id, owner, balance) VALUES (5, 'Eve', 50);, then CREATE INDEX accounts_owner_ix ON accounts (owner);, then ROLLBACK;. Does Eve's row exist afterward? Why? A second transaction then runs two updates, and the second one fails with ORA-02290. If you now issue ROLLBACK, what happens to the first update?
Show answer
Eve's row still exists. CREATE INDEX is DDL, and Oracle commits the open transaction before running it, so the insert was already permanent when ROLLBACK ran. In the second transaction, the failed statement is undone by itself, but the first update stays pending. Issuing ROLLBACK then discards the first update and ends the transaction.
Exercise
A session runs SELECT demo_seq.NEXTVAL FROM dual; and gets 7, then runs ROLLBACK;, then runs the same query again. What does it return, and why?
Show answer
It returns 8. Sequence values are not part of the transaction and are not undone by ROLLBACK.