Sub topic 11
Transactions Inside Triggers: No COMMIT and Autonomous Transactions
Assume you know basic SQL, PL/SQL blocks, exceptions, and transaction commands such as COMMIT and ROLLBACK. Oracle triggers follow the transaction of the SQL statement that fires them. Understanding this rule prevents partial updates and unexpected audit records.
A Trigger Runs in the Calling Transaction
A normal trigger does not start a separate transaction. Its inserts, updates, and deletes become part of the statement that caused the trigger to fire.
For example, this audit trigger writes a row when an employee's salary changes:
CREATE OR REPLACE TRIGGER employees_salary_aud
AFTER UPDATE OF salary ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_audit
(employee_id, old_salary, new_salary, changed_at)
VALUES
(:OLD.employee_id, :OLD.salary, :NEW.salary, SYSTIMESTAMP);
END employees_salary_aud;
/The audit insert remains uncommitted until the caller commits:
UPDATE employees
SET salary = salary * 1.05
WHERE department_id = 10;
ROLLBACK;The salary changes and the audit rows are rolled back together. This atomic behavior means the database does not keep an audit record for a change that the caller abandoned.
| Action | Result for a normal trigger |
|---|---|
| Caller commits | The triggering change and trigger work commit |
| Caller rolls back | The triggering change and trigger work roll back |
| Trigger raises an exception | The triggering statement fails and its changes roll back |
Why COMMIT Is Forbidden
A normal trigger cannot issue COMMIT, ROLLBACK, or transaction-control statements such as SAVEPOINT. The trigger must not end a transaction owned by its caller.
CREATE OR REPLACE TRIGGER invalid_commit_trigger
AFTER INSERT ON employees
FOR EACH ROW
BEGIN
INSERT INTO employee_audit (employee_id, changed_at)
VALUES (:NEW.employee_id, SYSTIMESTAMP);
COMMIT;
END invalid_commit_trigger;
/Oracle rejects or fails this trigger at runtime with ORA-04092, which reports that a commit is not allowed in a trigger.
The caller decides when the transaction ends. Keep normal trigger work atomic with the statement unless you have a specific reason to separate it.
Autonomous Transactions
An autonomous transaction is an independent transaction nested inside another transaction. A trigger declares one with PRAGMA AUTONOMOUS_TRANSACTION.
This pattern can record diagnostic information even when the original statement later fails:
CREATE OR REPLACE TRIGGER employees_change_log
AFTER UPDATE OF salary ON employees
FOR EACH ROW
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO employee_change_log
(employee_id, old_salary, new_salary, logged_at)
VALUES
(:OLD.employee_id, :OLD.salary, :NEW.salary, SYSTIMESTAMP);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END employees_change_log;
/The autonomous transaction must end with COMMIT or ROLLBACK. If it exits while still active, Oracle raises an error. The EXCEPTION block protects the autonomous transaction if the insert fails.
Autonomous transactions have risks. If the autonomous code tries to modify or lock data that the parent transaction already locked, the two transactions can wait for each other and produce a deadlock. Use this feature mainly for small, independent diagnostic or error logs. For business audit data that must match the transaction, use a normal trigger instead.
Exercise
An audit row must disappear whenever the salary update is rolled back. Should the audit trigger be autonomous?
Show answer
No. A normal trigger keeps the audit row in the same transaction, so the audit row rolls back with the salary update.