In Oracle's database products, an autonomous transaction is an independent transaction that is initiated by another transaction. It must contain at least one Structured Query Language (SQL) statement. Autonomous transactions allow a single transaction to be subdivided into multiple commit/rollback transactions, each of which will be tracked for auditing purposes.
When an autonomous transaction is called, the original transaction (calling transaction) is temporarily suspended. The autonomous transaction must commit or roll back before it returns control to the calling transaction. Once changes have been made by an autonomous transaction, those changes are visible to other transactions in the database.
Autonomous transactions can be nested. That is, an autonomous transaction can operate as a calling transaction, initializing other autonomous transactions within itself. In theory, there is no limit to the possible number of nesting levels.
'PRAGMA AUTONOMOUS_TRANSATION'
should be given in the declarative section of that block.
Syntax:
DECLARE PRAGMA AUTONOMOUS_TRANSACTION; . BEGIN[COMMIT|ROLLBACK] END; /
In the above syntax, the block has been made as an autonomous transaction.
DECLARE L_salary NUMBER; PROCEDURE nested_block IS PRAGMA AUTONOMOUS_TRANSACTION; BEGIN UPDATE emp SET salary=salary+15000 WHERE emp_no=1002; COMMIT; END; BEGIN SELECT salary INTO l_salary FROM emp WHERE emp_no=1001; Dbms_output.put_line('Before: Salary of 1001 is'||l_salary); SELECT salary INTO l_salary FROM emp WHERE emp_no=1002; Dbms output.put_line('Before: Salary of 1002 is'||1_salary): UPDATE emp SET salary=salary+5000 WHERE emp_no=1001; Nested_block; ROLLBACK; SELECT salary INTO l_salary FROM emp WHERE emp_no=1001; Dbms_output.put_line('After: Salary of 1001 is'||l_salary); SELECT salary INTO l_salary FROM emp WHERE emp_no=1002; Dbms_output.put_line('After: Salary of 1002 is'||l_salary); END: /
Output :
Before:Salary of 1001 is 15000 Before:Salary of 1002 is 10000 After:Salary of 1001 is 15000 After:Salary of 1002 is 25000
If you like dEexams.com and would like to contribute, you can write your article here or mail your article to admin@deexams.com . See your article appearing on the dEexams.com main page and help others to learn.