Sub topic 6
Why a failed statement does not abort the whole transaction
This chapter assumes you can run INSERT, UPDATE, and DELETE statements and know what COMMIT and ROLLBACK do. It also assumes you have the accounts table from the start of the tutorial, with Alice at 1000 and Bob at 200, committed. PL/SQL knowledge is needed only for the later examples.
The rule: statement-level atomicity
When a SQL statement fails in Oracle, Oracle undoes that one statement and leaves everything else in your transaction alone. The transaction stays open. Earlier changes remain pending, and you can still COMMIT or ROLLBACK them.
This property is called statement-level atomicity. A statement either completes fully or has no effect at all. A multi-row UPDATE that fails on its tenth row also loses the nine rows it had already changed.
Many developers expect the opposite. In PostgreSQL, an error inside a transaction puts it into an aborted state, and every later command fails with "current transaction is aborted" until you roll back. Oracle does not do this.
How Oracle does it
Before each DML statement runs, Oracle marks an implicit savepoint at the start of that statement. If the statement raises an error, Oracle uses undo data to roll back to that mark. You cannot name or reference this savepoint. It is a statement boundary kept internally, and it is discarded when the statement ends.
Savepoints you created yourself are unaffected. If you set SAVEPOINT s1 before a failing statement, s1 is still valid afterwards.
Worked example: a constraint violation
The balance >= 0 check on accounts lets us trigger a failure on demand.
-- Start: Alice 1000, Bob 200 (committed)
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
-- 1 row updated. Alice is now 900, uncommitted.
UPDATE accounts SET balance = balance - 300;
-- ORA-02290: check constraint (...) violated
-- Bob would go to -100, so the whole statement fails.
SELECT id, balance FROM accounts ORDER BY id;
-- 1 900 <- the first UPDATE is still pending
-- 2 200
ROLLBACK;
SELECT id, balance FROM accounts ORDER BY id;
-- 1 1000
-- 2 200Look at the second statement. Oracle may have processed Alice's row first and reduced it from 900 to 600 before reaching Bob's row. The SELECT shows 900, so that partial change was undone along with the failed statement. The order in which rows are visited is not guaranteed, but the outcome is: the statement changes nothing.
The first update survived the error. Only your ROLLBACK removed it.
What each kind of failure undoes
| Failure | What Oracle undoes | Transaction afterwards |
|---|---|---|
| DML violates a constraint (`ORA-02290`, `ORA-00001`, `ORA-02291`) | The failing statement only | Open, earlier work pending |
| Deadlock detected (`ORA-00060`) | The statement that was waiting | Open, earlier locks still held |
| PL/SQL block raises an unhandled exception | The whole block, as one statement | Open, work from before the block pending |
| PL/SQL exception handled inside the block | Nothing automatically | Open, work done before the error stays |
| DDL statement fails | Nothing to undo for the DDL | Already ended, because DDL committed first |
| Session crashes or is killed | The entire uncommitted transaction | Gone |
Two rows in this table deserve more detail.
Deadlocks
Suppose session A and session B each hold a row lock the other needs. Oracle detects the cycle and raises ORA-00060 in one session. It rolls back only the statement that was waiting, not that session's whole transaction. The session still holds the locks from its earlier statements, so the other session stays blocked until the first one issues ROLLBACK or COMMIT.
If your code catches ORA-00060 and simply retries the same statement, it can deadlock again. The usual fix is to roll back the whole transaction and start over, ideally after changing the code so that rows are always locked in the same order.
DDL failures
DDL commits the current transaction before it runs, even when the DDL then fails. If CREATE TABLE fails because the table already exists, your pending DML was committed anyway. Statement-level atomicity protects you from a partially applied statement. It does not protect work that DDL has already committed.
Statement atomicity inside PL/SQL
When a client sends an anonymous block to Oracle, the whole block counts as one statement. If an exception escapes the block, Oracle rolls back everything the block did and leaves earlier work alone.
-- Alice 1000, Bob 200, committed. No other pending work.
BEGIN
UPDATE accounts SET balance = balance + 50 WHERE id = 1;
UPDATE accounts SET balance = balance - 500 WHERE id = 2; -- violates the check
END;
/
-- ORA-02290: check constraint (...) violated
-- ORA-06512: at line 3
SELECT balance FROM accounts WHERE id = 1;
-- 1000, so the +50 from the first statement was undone tooThe first UPDATE succeeded inside the block, but the block as a whole failed, so Oracle rolled back to the state before the block started.
Now handle the exception inside the block:
SET SERVEROUTPUT ON
BEGIN
UPDATE accounts SET balance = balance + 50 WHERE id = 1;
UPDATE accounts SET balance = balance - 500 WHERE id = 2;
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('Error: ' || SQLERRM);
END;
/
-- The block ends normally, so nothing is rolled back for it.
SELECT balance FROM accounts WHERE id = 1;
-- 1050, the first UPDATE is still pending in your transaction
ROLLBACK;Here the second UPDATE was undone, because it was the statement that failed. The first UPDATE stays pending. The handler swallowed the error, so the block finished normally and Oracle had no reason to undo it.
Consequences for application code
Because the transaction survives a failed statement, it is the application's job to decide what happens next. Three patterns appear often.
- Abandon everything. Catch the error, issue
ROLLBACK, and report failure. This is correct for a unit of work such as a transfer, where both updates must happen or neither. - Skip the bad item and continue. Put a savepoint before each item and
ROLLBACK TO SAVEPOINTon error, as in the per-row loop from the savepoints chapter. Statement atomicity already cleaned up the failed statement, but a savepoint is needed when one item involves several statements. - Retry. Retry only for transient errors such as a deadlock. Roll back first if the work spans several statements.
Scripts are a common source of trouble. SQL*Plus keeps running after an error by default, so a script of ten statements with a final COMMIT commits the nine that succeeded. To stop instead, put this at the top:
WHENEVER SQLERROR EXIT FAILURE ROLLBACKThe script then exits at the first error and rolls back the open transaction. SQL*Plus normally commits pending work when you exit, so the explicit ROLLBACK matters.
Letting a statement succeed despite bad rows
For bulk operations, DML error logging offers a different trade-off. The statement is allowed to succeed, and rows that would have raised an error are written to an error table instead.
EXEC DBMS_ERRLOG.CREATE_ERROR_LOG('ACCOUNTS');
UPDATE accounts
SET balance = balance - 300
LOG ERRORS INTO err$_accounts ('debit run') REJECT LIMIT UNLIMITED;
-- Rows that would break the check are skipped and logged.
-- The other rows are updated.
SELECT ora_err_mesg$, id FROM err$_accounts WHERE ora_err_tag$ = 'debit run';
ROLLBACK;The statement now reports success, so you must read the error table to learn what was rejected. It also moves the decision from "all or nothing" to "whatever succeeded", so use it only when partial application is acceptable.
The deferred-constraint case is the one exception in this chapter where a single violation costs you the whole transaction. The failure is raised during commit, so there is no statement left to undo.
Exercise
Your session has run an UPDATE on Alice's row that succeeded (uncommitted). A second UPDATE then fails with ORA-02290. You now run COMMIT. What happens to Alice's first update, and why? What would change if the second statement had been a failing CREATE TABLE instead?
Show answer
Alice's first update is committed. The failed UPDATE was rolled back to its own start by statement-level atomicity, so the transaction was still open with the first update pending, and COMMIT saves it. If the failing statement had been CREATE TABLE, Oracle would already have committed the first update before attempting the DDL, so it would be permanent even before your COMMIT.
Exercise
An anonymous block runs two UPDATEs, and the second raises an exception with no handler. Before the block, your session had one pending, uncommitted INSERT. After the error, which changes are still pending?
Show answer
Only the INSERT from before the block. Oracle treats the whole block as one statement and rolls back everything the block did, including its first successful UPDATE, but it leaves earlier work in the transaction untouched.