Monday 11 May 2015

PRAGMA AUTONOMOUS_TRANSACTION in PLSQL

    An autonomous transaction is an independent transaction started by another transaction i.e. the main transaction. Autonomous transactions do SQL operations and commit or rollback without commit or rolling back the main transaction.














The following types of PL/SQL blocks can be defined as autonomous transactions:
1) Stored procedures and functions.
2) Local procedures and functions defined in a PL/SQL declaration block.
3) Packaged procedures and functions.
4) Triggers
5) schema-level anonymous pl/sql blocks

Advantages of Autonomous transaction:
1) After started the autonomous transaction is fully independent.
2) It shares no locks or resources with main transaction.
3) It helps to build modular, re-usable components.

Lets look into in detail with an example
1) Create a table


2) Insert a value to the table test_update


3) Create another table to insert values in child procedure call


4)Create a main procedure


5)Create child procedure which has commit statement and declared as PRAGMA AUTONOMOUS_TRANSACTION


6)Check the values in the table test_update


7)Call the Main procedure to update test_update , name from RG to RG HEGDE



8) Check the table test_tab

The value will be inserted from child procedure and it is commited

9)Check the table test_update.

The ename column will not be updated. It shows that the child procedure executed independently.

Verification:
1)Re-Create the child procedure without PRAGMA AUTONOMOUS_TRANSACTION.


2) Call the main procedure


3) Now check the table test_update

The ename column will be changed from RG to RG HEGDE

No comments: