Sub topic 14
Autonomous Transactions for Logging and Auditing
Assume you know basic SQL and PL/SQL blocks, including exception handlers. This chapter focuses on one problem: recording an error or audit event even when the main transaction is rolled back.
An autonomous transaction is an independent transaction started by a PL/SQL subprogram. It has its own COMMIT and ROLLBACK. The calling transaction pauses while the autonomous code runs, but the two transactions commit separately.
Why use an autonomous transaction?
Suppose an order procedure inserts several rows and then fails. The caller rolls back the order. A normal audit insert in the same transaction would also disappear. An autonomous logging procedure can commit the audit row before the main transaction rolls back.
| Main transaction | Autonomous logging transaction |
|---|---|
| May be rolled back after an error | Can commit the log independently |
| Owns the business changes | Owns the audit or error record |
| Must not depend on the logger's commit | Must finish with `COMMIT` or `ROLLBACK` |
An autonomous transaction is synchronous. The logging procedure finishes before control returns to its caller. It does not run in the background.
Declaring an autonomous logger
Declare the pragma in the declarative section of the procedure. Every execution path must end with a transaction control statement. The following example creates an error log and a procedure that writes to it.
CREATE TABLE error_log (
log_id NUMBER GENERATED ALWAYS AS IDENTITY,
log_time TIMESTAMP DEFAULT SYSTIMESTAMP NOT NULL,
error_code NUMBER,
error_text VARCHAR2(4000),
backtrace VARCHAR2(4000)
);
CREATE OR REPLACE PROCEDURE log_error (
p_error_code IN NUMBER,
p_error_text IN VARCHAR2,
p_backtrace IN VARCHAR2
) AUTHID DEFINER
IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO error_log (
error_code,
error_text,
backtrace
)
VALUES (
p_error_code,
p_error_text,
p_backtrace
);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
RAISE;
END log_error;
/The COMMIT makes the log visible to other sessions, even if the caller later rolls back. The exception handler rolls back any partial work and re-raises the logging error. This prevents a failed logger from silently hiding a problem.
Calling the logger from an exception handler
A business procedure can record the current error and then re-raise it:
BEGIN
UPDATE orders
SET status = 'SHIPPED'
WHERE order_id = 1001;
-- Other business work could fail here.
RAISE_APPLICATION_ERROR(-20010, 'Shipping service unavailable');
EXCEPTION
WHEN OTHERS THEN
log_error(
SQLCODE,
SQLERRM,
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
);
RAISE;
END;
/The final RAISE preserves the original error for the caller. The log remains after the caller rolls back.
Failure modes and design rules
- The autonomous routine cannot see uncommitted changes made by its caller.
- Do not have it update a row that the parent transaction has locked. This can cause a deadlock.
- Keep the routine short and limit it to tables reserved for logging or auditing.
- Always commit successful work and roll back failed work.
- Test whether logging failures should replace the original error or be handled separately.
Exercise
Why does an autonomous logger need its own COMMIT?
Show answer
Its work belongs to a separate transaction. Without a COMMIT or ROLLBACK, Oracle raises an error when the procedure exits with an active autonomous transaction.