How to Fix ORA-06519: Active Autonomous Transaction Detected and Rolled Back in Oracle

End autonomous transactions with COMMIT or ROLLBACK before the routine returns, and see why the logged row survives the caller's rollback.

ORA-06519 is raised when a procedure or function declared with PRAGMA AUTONOMOUS_TRANSACTION ends while its own transaction is still open. An autonomous routine runs its own transaction, separate from the caller's, and must finish it with COMMIT or ROLLBACK before returning. If it does not, Oracle rolls back its changes and raises ORA-06519. Add the COMMIT, or ROLLBACK, at the end of the routine and in its exception handlers.

Reproduce the Error

A logging procedure inserts a row in its own transaction, but never commits:

Example:

create table demo_log (logged_at timestamp default systimestamp, msg varchar2(200));
create or replace procedure demo_write_log (p_msg varchar2) is
  pragma autonomous_transaction;
begin
  insert into demo_log (msg) values (p_msg);
end;                                          -- no COMMIT or ROLLBACK
/
exec demo_write_log('payment failed')
select count(*) from demo_log;

Output:

Table DEMO_LOG created.

Procedure DEMO_WRITE_LOG compiled

BEGIN demo_write_log('payment failed'); END;
*
ERROR at line 1:
ORA-06519: active autonomous transaction detected and rolled back
ORA-06512: at "NIMBUS.DEMO_WRITE_LOG", line 5
ORA-06512: at line 1

   COUNT(*)
___________
          0

The log row was rolled back: the table is empty.

Fix: End the Autonomous Transaction

Example:

create or replace procedure demo_write_log (p_msg varchar2) is
  pragma autonomous_transaction;
begin
  insert into demo_log (msg) values (p_msg);
  commit;                                     -- ends the autonomous transaction
end;
/
exec demo_write_log('payment failed')
rollback;                 -- the caller's rollback does not undo the log row
select msg from demo_log;

Output:

Procedure DEMO_WRITE_LOG compiled

PL/SQL procedure successfully completed.

Rollback complete.

MSG
_________________
payment failed

The routine commits its own row. The caller's ROLLBACK afterward does not remove it, which is the point of logging in an autonomous transaction.

Things to Know

  • An exception that leaves the routine rolls back its open transaction, so commit the log row before any step that can fail.
  • An autonomous transaction does not see the caller's uncommitted changes, and it can wait on rows the caller has locked.
  • Use autonomous transactions for logging and auditing, not for ordinary business changes, which belong in the caller's transaction.

Related Guides

Conclusion

ORA-06519 means an autonomous routine returned with its transaction still open. End it with COMMIT or ROLLBACK on every path, including exception handlers.

Vinish Kapoor
Vinish Kapoor

An Oracle ACE, author of four books on Oracle APEX, SQL and PL/SQL, and Oracle Forms, and a software developer building Oracle database applications since 2001.

guest

0 Comments
Oldest
Newest Most Voted