PL/SQL Pragma
Pragma –
Pragma is nothing but the compiler directive which simply conveys the information to the compiler. Because the Pragma is the complier directive so the processed at compile time not run time.
AUTONOMOUS_TRANSACTION Pragma
AUTONOMOUS_TRANSACTION Pragma instructs to the compiler to mark a transaction autonomous, or you can independent. An Autonomous transaction is the independent transactions which runs by the another transaction or main transaction. An autonomous transaction suspends the main transaction in which they are calling, do the SQL statements, commit or rollback then according to the user’s need then after completing resume the main transaction.
Commit or Rollback only works for the statements which are inside the calling transaction so that they are independent to the main transaction. It does not share any lock, resources and any commit or rollback dependence with the main transaction.
If you are using it with the triggers then unlike normal triggers these autonomous triggers can perform the DDL operation by using the native dynamic SQL. If you set the isolation level of the main transaction to SERIALIZABLE, as follows, changes made by its autonomous transactions are not visible to the main transaction hen it resumes:
See, if the main transaction perform the rollback at any save point after the autonomous block then it will not effect the changes made by the autonomous transaction because the autonomous transaction is fully independent from it’s main transaction.
Limitations –
You can not use the Pragma in the Subprogram that resides in the package. You can only user Pragma in the individual subprogram. You can use Pragma anywhere in the declarative part of the PL/SQL Block but always use it on the top position in the declarative part for readability.
Consider the following block of code –
– Main Procedure (Caller Procedure)
create or replace procedure MainProc as
begin
update myemployeemaster set employeename=‘vinny’ where employeename=‘Ashish’;
SimpleProc; – Calling an ordinary procedure
rollback;
exception
when others then null;
end;
– Procedure without Autonomous Transaction Block (Calee Procedure)
create or replace procedure SimpleProc as
begin
update myemployeemaster set employeename=‘Vikram’ where employeename=‘Pawan Kumar’;
commit;
exception
when others then null;
end;
Explanation-
In the above example the MainProc updates the MyEmployeeMaster table and update the name Ashish to Vinny. But After a SimpleProc call the procedure want to rollback his transaction. But when the SimpleProc procedure called the control of MaiProc goes into the SiimpleProc. In the SimpleProc MyEmployeeMaster is again updating the name Pawan Kumar with the name Vikram and then it commit its transactions. After the completing of SimpleProc the control again goes to the MainProc Procedure and then want to rollback all the changes done by it. But See, The SimpleProc Transaction has already committed the whole transactions. See the effect in the table MyEmployeeMaster, You will see that all the transactions are committed even of the MainProc Procedure.
It happened because the in the both procedure only single transaction is working, both are the part of a single transaction. So If you want to work on different Procedure for different transactions then you can use the Autonomous_Transaction Pragma. This Pragma will direct to the compiler for doing the different Transaction for the block where it defined.
Now the following Block of code is modified by using Autonomous_Transaction Pragma –
– Main Procedure (Caller Procedure)
create or replace procedure MainProc as
begin
update myemployeemaster set employeename=‘Vinay’ where employeename=‘Vikram’;
AutoProc; – Calling an ordinary procedure
rollback;
exception
when others then null;
end;
– Procedure without Autonomous Transaction Block (Calee Procedure)
create or replace procedure AutoProc as
pragma autonomous_transaction;
begin
update myemployeemaster set employeename=‘Pawan Kumar’ where employeename=‘vinny’;
commit;
exception
when others then null;
end;
Explanation-
Now consider the about block of code. In this code AutoProc Procedure difines the Pragma Autonomous_Transaction. So that it has a different transaction for this procedure . When a Commit operation is performed then it will only commit those transactions which are rekated to this procedure. And when the control goes back to the MainProc then it will rollback the transactions of the MainProc Procedure. So As a result name vinny in the MyEmployeeMaster is changed with the name Pawan Kumar. But Vikram has not changed with the Vinay.