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:
Thank you very much. S.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With