Sub topic 6
Why a Failed Statement Does Not Abort the Whole Transaction
Assume you know basic SELECT, INSERT, UPDATE, and DELETE, and that you can connect to Oracle with SQL*Plus, SQL Developer, or SQLcl. This chapter focuses on statement-level atomicity, which means each SQL statement succeeds completely or has no effect.
Statement-level atomicity
Oracle treats one SQL statement as one unit of work. If the statement fails, Oracle undoes the changes made by that statement. It does not automatically undo earlier successful statements in the same transaction.
For example, this UPDATE tries to subtract 250 from both accounts. The balance check prevents account 2 from becoming negative:
CREATE TABLE statement_demo_accounts (
id NUMBER PRIMARY KEY,
owner VARCHAR2(50) NOT NULL,
balance NUMBER(12,2) NOT NULL CHECK (balance >= 0)
);
INSERT INTO statement_demo_accounts VALUES (1, 'Alice', 1000);
INSERT INTO statement_demo_accounts VALUES (2, 'Bob', 200);
COMMIT;
UPDATE statement_demo_accounts
SET balance = balance - 250;
SELECT id, balance
FROM statement_demo_accounts
ORDER BY id;The UPDATE fails with a constraint error. Neither row is changed. Alice keeps a balance of 1000, and Bob keeps a balance of 200.
The failed statement does not roll back the CREATE TABLE or the INSERT statements. Those statements were already committed. If earlier work was still uncommitted, that work would remain part of the open transaction.
A failed statement inside a transaction
A transaction can continue after a statement failure:
UPDATE statement_demo_accounts
SET balance = balance + 10
WHERE id = 1;
UPDATE statement_demo_accounts
SET balance = balance - 250
WHERE id = 2; -- fails because the balance would be negative
UPDATE statement_demo_accounts
SET balance = balance + 5
WHERE id = 2;
COMMIT;
SELECT id, balance
FROM statement_demo_accounts
ORDER BY id;The first update succeeds. The second update fails and contributes no changes. The third update succeeds. COMMIT saves the first and third updates together.
A client tool might stop executing a script after an error even though Oracle has left the transaction open. That is client behavior, not a database rollback. Check the transaction state and issue COMMIT or ROLLBACK deliberately.
SQL statements and PL/SQL exceptions
In PL/SQL, a failed SQL statement raises an exception. An exception is a PL/SQL error signal that transfers control to an exception handler or ends the current block.
If a handler catches the error, the transaction remains open unless the handler issues ROLLBACK or ROLLBACK TO SAVEPOINT. This lets a program recover from one bad operation, but the handler must prevent invalid data from being used later.
SAVEPOINT before_change;
BEGIN
UPDATE statement_demo_accounts
SET balance = balance - 250
WHERE id = 2;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT before_change;
DBMS_OUTPUT.PUT_LINE('Change rejected: ' || SQLERRM);
END;
/The savepoint gives the handler a known point to return to. For larger PL/SQL loops, create a savepoint before each independent item and roll back to it when that item fails.
Exercise
An account update succeeds, the next update violates a check constraint, and a third update succeeds. What does COMMIT save?
Show answer
It saves the first and third updates. The failed second statement contributes no changes, unless the application explicitly rolls back the transaction.