Sub topic 11
Transaction Control in PL/SQL and Exception Handling
Prerequisites and transaction ownership
This chapter assumes that you know basic INSERT, UPDATE, DELETE, and PL/SQL block syntax. You should also understand COMMIT, ROLLBACK, and SAVEPOINT from earlier chapters.
A PL/SQL block does not become a separate transaction automatically. Its SQL statements normally use the transaction of the session that called the block. A nested PL/SQL block is also not an independent transaction. Decide which layer owns the transaction before adding transaction control.
- A batch procedure may commit a complete business unit of work.
- A reusable procedure usually leaves
COMMITand fullROLLBACKto its caller. - An exception handler should undo work that cannot safely remain, then either handle the error or re-raise it.
Handling an error with a savepoint
The following block transfers money and protects the transfer with a savepoint. If either update fails, the handler undoes both updates and passes the error to the caller.
BEGIN
SAVEPOINT before_transfer;
UPDATE accounts
SET balance = balance - 100
WHERE id = 1;
IF SQL%ROWCOUNT <> 1 THEN
RAISE_APPLICATION_ERROR(-20001, 'Source account was not found');
END IF;
UPDATE accounts
SET balance = balance + 100
WHERE id = 2;
IF SQL%ROWCOUNT <> 1 THEN
RAISE_APPLICATION_ERROR(-20002, 'Destination account was not found');
END IF;
-- The caller owns the final transaction decision.
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT before_transfer;
RAISE;
END;
/SQL%ROWCOUNT reports how many rows the most recent DML statement affected. RAISE_APPLICATION_ERROR creates an application error with a number in the range -20000 through -20999.
ROLLBACK TO SAVEPOINT removes changes made after the savepoint but keeps the transaction open. RAISE sends the current exception to the calling program without losing its original error details. The caller can then issue COMMIT or a full ROLLBACK.
A failed SQL statement is statement-atomic: Oracle undoes that statement. However, earlier work in the same transaction can remain. A savepoint gives the handler a precise boundary when several statements form one logical operation.
Processing several rows safely
A savepoint can be created for each item when one bad item should not cancel the whole batch.
SET SERVEROUTPUT ON
BEGIN
FOR r IN (SELECT id FROM accounts ORDER BY id) LOOP
SAVEPOINT before_account;
BEGIN
UPDATE accounts
SET balance = balance - 100
WHERE id = r.id;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK TO SAVEPOINT before_account;
DBMS_OUTPUT.PUT_LINE(
'Skipped account ' || r.id || ': ' || SQLERRM
);
END;
END LOOP;
COMMIT;
END;
/The inner block handles each row locally. The outer block commits successful changes together. If the process itself fails outside the inner handler, the outer block does not automatically guarantee the desired business outcome. Add an outer handler when the batch needs a full rollback and a clear error report.
Common mistakes
- Assuming that leaving a PL/SQL block commits its changes. It does not.
- Calling
COMMITinside a reusable procedure and unexpectedly committing the caller's unrelated work. - Using full
ROLLBACKwhen only the current operation should be undone. - Forgetting that DDL inside the block causes implicit commits.
- Catching an exception, printing it, and continuing when the data may no longer satisfy the business rule.
Exercise
Why does the transfer example use ROLLBACK TO SAVEPOINT and then RAISE instead of only using ROLLBACK?
Show answer
The savepoint preserves earlier caller work, while RAISE tells the caller that the transfer failed so it can choose COMMIT or full ROLLBACK.