ámbito de transacción autónomo y generación de errores

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() método.

Entonces tengo 2 preguntas:

  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.

preguntado el 28 de agosto de 12 a las 14:08

1 Respuestas

  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.
    

Respondido 28 ago 12, 15:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.