Autonomous Transaction

Oracle Autonomous Transaction

oracle_autonomous_transaction

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.

  • This autonomous transaction can be specified at subprogram level.
  • To make any subprogram to work in a different transaction, the keyword 'PRAGMA AUTONOMOUS_TRANSATION' should be given in the declarative section of that block.
  • It will instruct that compiler to treat this as the separate transaction and saving/discarding inside this block will not reflect in the main transaction.
  • Issuing COMMIT or ROLLBACK is mandatory before going out of this autonomous transaction to the main transaction because at any time only one transaction can be active.
  • So once we made an autonomous transaction we need to save it and complete the transaction then only we can move back to the main transaction.

Syntax:

DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
.
BEGIN

[COMMIT|ROLLBACK]
END;
/

In the above syntax, the block has been made as an autonomous transaction.

Example :

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 also 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.


Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Python if , elif and else

Python Conditions and If statements

  • 0
Python for beginners

Learning Python Part 1

  • 3
Struct Alignment and Padding

Struct Alignment and Padding in C++ And C

  • 0
Friend function

Friend function C++

  • 0
Pointers

C++ Pointers

  • 0
Structures

C++ Structures

  • 0
Types of Inheritance in C++

Inheritance and access specifiers C++

  • 0
Java date pattern

Java Date Pattern Syntax

  • 0
Java Date and Calendar

Java Date formats

  • 0
JAVA Data Type

Data types in Java

  • 0
Java unreachable code

Unreachable Code Error in Java

  • 0

Post Comment

Comments(0)

WEB TECHNOLOGY

Articles

×

Forgot Password

Please enter your email address below and we will send you information to change your password.