Independent transaction with Autonomous transaction in Oracle

The procedure must act as an independent transaction irrespective of its parent transaction

Autonomous Transaction provides a way to treat it as a separate transaction and save/discard that transaction without affecting the primary session transaction. Suppose some Procedure/package calls another procedure/function/package then you want to save the data irrespective of its parent procedure transaction whether it rollback or commit. You only want to commit in that case you have to use Autonomous transaction to treat as a separate transaction.

Syntax for using Autonomous Transaction:

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION; 
.
BEGIN
<executin_part>
[COMMIT|ROLLBACK]
END;

Note:

  1. The use of commit / Rollback is a must after the autonomous transaction.
  2. PRAGMA AUTONOMOUS_TRANSATION should be given in the declarative section of that block to start an independent transition.

Example:

create or replace procedure update_salary
is
pragma autonomous_transaction;
begin
update test.emp set salary = salary +100 where employee_id = 100;
commit;
end;
/

Error handle similar case: https://smarttechways.com/2020/04/02/ora-06519-active-autonomous-transaction-detected-and-rolled-back/

This entry was posted in Oracle on by .
Unknown's avatar

About SandeepSingh

Hi, I am working in IT industry with having more than 15 year of experience, worked as an Oracle DBA with a Company and handling different databases like Oracle, SQL Server , DB2 etc Worked as a Development and Database Administrator.

Leave a Reply