Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

autonomous transaction scope and error raising

I'm doubting a bit. Let's assume this package's procedures:

PROCEDURE ERR_MANAGER(I_ERRM IN VARCHAR2) IS
BEGIN
    ROLLBACK;
    --DO SOME STUFF
END ERR_MANAGER;

PROCEDURE test IS 
    PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN

    test2;
    COMMIT;
EXCEPTION WHEN OTHERS THEN ERR_MANAGER(SQLERRM);
END test;


PROCEDURE test2 IS
BEGIN

    --DO SOME TRANSACTIONNAL DML

    RAISE_APPLICATION_ERROR(-20001, 'ERR'); --for the test purpose, in reality this could be any actual error

END test2;

So, as you can see there's an error in test2(), which is going to raise up to test(), and then be handled in the err_manager() method.

So I have 2 questions:

  1. what's the scope of err_manager()? Is it still within the autonomous transaction? I guess so, since it is only a function call, but I'd like to be sure
  2. what's happening if you exit an autonomous transaction brutally because of an error raising to upper levels, without proceeding any kind of commit or rollback?

Thank you very much. S.

like image 481
Sebas Avatar asked Aug 28 '12 13:08

Sebas


People also ask

What are autonomous transactions?

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.

Which are true about autonomous transactions?

Remember, autonomous transactions are fully independent of the main transaction. If an autonomous transaction attempts to access a resource held by the main transaction (which cannot resume until the autonomous routine exits), a deadlock can occur.

What is autonomous transaction in PL SQL?

When you define a PL/SQL block as an autonomous transaction, you isolate the DML in that block from the caller's transaction context. That block becomes an independent transaction that is started by another transaction, referred to as the main transaction.

What is pragma in Oracle with example?

The PRAGMA keyword is used to signify that the remainder of the PL/SQL statement is a pragma, or directive, to the compiler. Also called apseudoinstruction, a pragma simply passes information to the compiler rather than getting transformed into a particular execution.


1 Answers

  1. The transaction scope of the execution of the err_manager procedure is the calling autonomous transaction, you are correct.

    Procedures and functions inherit their calling transactions unless they are themselves autonomous transactions.

  2. When an autonomous transaction raises an unhandled error, it rollbacks its changes and the error propagates to the calling application. Here's a test:

    SQL> CREATE TABLE t (id number);
    
    Table created.
    
    SQL> DECLARE
      2     l NUMBER;
      3     PROCEDURE p IS
      4        pragma autonomous_transaction;
      5     BEGIN
      6        insert into t values (1);
      7        raise_application_error(-20001, 'rollback?');
      8     END;
      9  BEGIN
     10     p;
     11  EXCEPTION
     12     WHEN OTHERS THEN
     13        DBMS_OUTPUT.put_line('error catched:' || sqlcode);
     14        SELECT COUNT(*) INTO l FROM t;
     15        DBMS_OUTPUT.put_line('lines in t: ' || l);
     16  END;
     17  /
    error catched:-20001
    lines in t: 0
    
    PL/SQL procedure successfully completed.
    
like image 175
Vincent Malgrat Avatar answered Sep 21 '22 01:09

Vincent Malgrat