8i | 9i | 10g | 11g | 12c | 13c | 18c | 19c | 21c | 23c | Misc | PL/SQL | SQL | RAC | WebLogic | Linux

Home » Articles » Misc » Here

Autonomous Transactions

Autonomous transactions allow you to leave the context of the calling transaction, perform an independant transaction, and return to the calling transaction without affecting it's state. The autonomous transaction has no link to the calling transaction, so only commited data can be shared by both transactions.

The following types of PL/SQL blocks can be defined as autonomous transactions:

The easiest way to understand autonomous transactions is to see them in action. To do this, we create a test table and populate it with two rows. Notice that the data is not commited.

create table at_test (
  id           number       not null,
  description  varchar2(50) not null
);

insert into at_test (id, description) values (1, 'Description for 1');
insert into at_test (id, description) values (2, 'Description for 2');

select * from at_test;

        ID DESCRIPTION
---------- --------------------------------------------------
         1 Description for 1
         2 Description for 2

2 rows selected.

SQL>

Next, we insert another 8 rows using an anonymous block declared as an autonomous transaction, which contains a commit statement.

declare
  pragma autonomous_transaction;
begin
  for i in 3 .. 10 loop
    insert into at_test (id, description)
    values (i, 'Description for ' || i);
  end loop;
  commit;
end;
/

PL/SQL procedure successfully completed.

select * from at_test;

        ID DESCRIPTION
---------- --------------------------------------------------
         1 Description for 1
         2 Description for 2
         3 Description for 3
         4 Description for 4
         5 Description for 5
         6 Description for 6
         7 Description for 7
         8 Description for 8
         9 Description for 9
        10 Description for 10

10 rows selected.

SQL>

As expected, we now have 10 rows in the table. If we now issue a rollback statement we get the following result.

rollback;
select * from at_test;

        ID DESCRIPTION
---------- --------------------------------------------------
         3 Description for 3
         4 Description for 4
         5 Description for 5
         6 Description for 6
         7 Description for 7
         8 Description for 8
         9 Description for 9
        10 Description for 10

8 rows selected.

SQL>

The 2 rows inserted by our current session (transaction) have been rolled back, while the rows inserted by the autonomous transactions remain. The presence of the PRAGMA AUTONOMOUS_TRANSACTION compiler directive made the anonymous block run in its own transaction, so the internal commit statement did not affect the calling session. As a result rollback was still able to affect the DML issued by the current statement.

Autonomous transactions are commonly used by error logging routines, where the error messages must be preserved, regardless of the the commit/rollback status of the transaction. For example, the following table holds basic error messages.

create table error_logs (
  id             number(10)     not null,
  log_timestamp  timestamp      not null,
  error_message  varchar2(4000),
  constraint error_logs_pk primary key (id)
);

create sequence error_logs_seq;

We define a procedure to log error messages as an autonomous transaction.

create or replace procedure log_errors (p_error_message  in  varchar2) as
  pragma autonomous_transaction;
begin
  insert into error_logs (id, log_timestamp, error_message)
  values (error_logs_seq.nextval, systimestamp, p_error_message);
  commit;
end;
/

The following code forces an error, which is trapped and logged.

begin
  insert into at_test (id, description)
  values (998, 'Description for 998');

  -- Force invalid insert.
  insert into at_test (id, description)
  values (999, null);
exception
  when others then
    log_errors (p_error_message => sqlerrm);
    rollback;
end;
/

PL/SQL procedure successfully completed.

select * from at_test where id >= 998;

no rows selected

select * from error_logs;

        ID LOG_TIMESTAMP
---------- ---------------------------------------------------------------------------
ERROR_MESSAGE
----------------------------------------------------------------------------------------------------
         1 28-FEB-2006 11:10:10.107625
ORA-01400: cannot insert NULL into ("TIM_HALL"."AT_TEST"."DESCRIPTION")


1 row selected.

SQL>

From this we can see that the LOG_ERRORS transaction was separate to the anonymous block. If it weren't, we would expect the first insert in the anonymous block to be preserved by the commit statement in the LOG_ERRORS procedure.

Be careful how you use autonomous transactions. If they are used indiscriminately they can lead to deadlocks, and cause confusion when analyzing session trace. To hammer this point home, here's a quote from Tom Kyte.

"... in 999 times out of 1000, if you find yourself "forced" to use an autonomous transaction - it likely means you have a serious data integrity issue you haven't thought about.

Where do people try to use them?

Error logging - OK.

Almost everything else - not OK."

For more information see:

Hope this helps. Regards Tim...

Back to the Top.