Answered Questions

  • Oracle triggers

    can we issue rollback, commit in the trigger body. if we issue what is the result

    Star Read Best Answer

    Editorial / Best Answer

    binurajnair  

    • Member Since Feb-2008 | Feb 28th, 2008


          An autonomous transaction is an independent transaction which can be committed independent of other transactions. An autonomous transaction will be committed with out committing the other non-autonomous transactions are committed.

    The following exercise will help you to understand the difference between autonomous and normal transactions.

    Create a table for the exercise.

    create table test_table (a varchar2(50));

    Create a non-autonomous procedure which will insert one row to this table.

    CREATE OR REPLACE PROCEDURE non_autonomous_proc
    IS
    BEGIN
       INSERT INTO test_table
       VALUES      ('Non Autnomous Insert');

       COMMIT;
    END;


    Now execute the following code

    DECLARE
    BEGIN
       INSERT INTO test_table
       VALUES      ('Before Non Autnomous Insert');

       non_autonomous_proc;

       INSERT INTO test_table
       VALUES      ('After Non Autnomous Insert');

       ROLLBACK;
    END;


    Now query the table

    select * from test_table;


    As expected 2 rows will be retrieved

    A
    --------------------------------------------------
    Before Non Autnomous Insert
    Non Autnomous Insert


    Now we will see the case of autonomous transaction.

    Clear the test_table

    delete from test_table;
    commit;

    Create an autonomous procedure which will insert one row to this table.

    CREATE OR REPLACE PROCEDURE autonomous_proc
    IS
     PRAGMA AUTONOMOUS_TRANSACTION;
    BEGIN
       INSERT INTO test_table
       VALUES      ('Autnomous Insert');

       COMMIT;
    END;

    Now Execute the following code

    DECLARE
    BEGIN
       INSERT INTO test_table
       VALUES      ('Before Autnomous Insert');

       autonomous_proc;

       INSERT INTO test_table
       VALUES      ('After Autnomous Insert');

       ROLLBACK;
    END;

    And query the table


    select * from test_table;


    This will show only one row

    A
    --------------------------------------------------
    Autnomous Insert


    Conclusion

    1) The commit statement in the autonomous procedure will commit the DML operations in the autonomous procedure without commiting the transactions before that.

    2) The rollback statement caused both the inserts before and after the autonomous transaction to be rolled back, but not the autonomous transaction