Sub topic 14
Logging Errors That Survive a Rollback
Assume you know PL/SQL block structure, exception handlers, SQLCODE, SQLERRM, and basic COMMIT and ROLLBACK behavior. This chapter focuses on keeping an error log even when the transaction that caused the error is rolled back.
Why a normal log insert can disappear
An INSERT into an error table normally belongs to the caller's transaction:
INSERT INTO error_log (message) VALUES ('Update failed');
ROLLBACK;The rollback removes both the failed work and the log row. This is often undesirable because the log should explain why the transaction failed.
An autonomous transaction is an independent transaction started by a PL/SQL subprogram. Its changes need their own COMMIT or ROLLBACK. A committed autonomous transaction is not undone when the caller later rolls back.
Creating an autonomous logger
The following table stores a timestamp and diagnostic information:
CREATE TABLE error_log (
logged_at TIMESTAMP DEFAULT SYSTIMESTAMP,
error_code NUMBER,
message VARCHAR2(4000),
error_stack VARCHAR2(4000),
error_backtrace VARCHAR2(4000)
);Declare PRAGMA AUTONOMOUS_TRANSACTION in the logging procedure. The procedure inserts the row and commits it before returning.
CREATE OR REPLACE PROCEDURE log_error (
p_code IN NUMBER,
p_message IN VARCHAR2,
p_stack IN VARCHAR2,
p_backtrace IN VARCHAR2
) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO error_log (
error_code, message, error_stack, error_backtrace
)
VALUES (
p_code, p_message, p_stack, p_backtrace
);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
END log_error;
/The ROLLBACK in the logger handles a failure inside the logging transaction. This example does not raise that secondary failure, so it does not replace the original application error. Production systems should also send logger failures to an operational monitoring system.
Calling the logger from an exception handler
The caller can collect details and then re-raise the original exception:
BEGIN
UPDATE employees
SET salary = salary / 0
WHERE employee_id = 100;
EXCEPTION
WHEN OTHERS THEN
log_error(
SQLCODE,
SQLERRM,
DBMS_UTILITY.FORMAT_ERROR_STACK,
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE
);
ROLLBACK;
RAISE;
END;
/The update fails. The handler starts the autonomous logger, which commits the diagnostic row. The caller then rolls back its own transaction and uses RAISE to preserve the original error for its caller.
Limits and common mistakes
- The autonomous procedure must end with
COMMITorROLLBACK. Otherwise Oracle raises an error when it exits. - Do not make the logger update rows locked by the caller. The autonomous transaction may wait for the caller, while the caller waits for the logger, causing a deadlock.
- Keep the log table and logger simple. Avoid triggers or foreign keys that require uncommitted caller data.
- Log before
RAISEif the original exception must still reach the caller. - Do not use autonomous transactions as a substitute for sound transaction design.
Exercise
Why does the row inserted by log_error remain after the caller executes ROLLBACK?
Show answer
log_error runs in a separate autonomous transaction and commits its insert independently of the caller's transaction.