Thursday, July 2, 2015

The AUTONOMOUS_TRANSACTION pragma




The AUTONOMOUS_TRANSACTION pragma
PRAGMA
Signifies that the statement is a pragma (compiler directive). Pragmas are processed at compile time, not at run time. They pass information to the compiler.
Usage Notes
You can apply this pragma to:
  • Top-level (not nested) anonymous PL/SQL blocks
  • Local, standalone, and packaged functions and procedures
  • Methods of a SQL object type
  • Database trigger.

    You cannot apply this pragma to an entire package or an entire an object type. Instead, you can apply the pragma to each packaged subprogram or object method.
    You can code the pragma anywhere in the declarative section. For readability, code the pragma at the top of the section.
    Once started, an autonomous transaction is fully independent. It shares no locks, resources, or commit-dependencies with the main transaction. You can log events, increment retry counters, and so on, even if the main transaction rolls back.
    Unlike regular triggers, autonomous triggers can contain transaction control statements such as COMMIT and ROLLBACK, and can issue DDL statements (such as CREATE and DROP) through the EXECUTE IMMEDIATE statement.
    Changes made by an autonomous transaction become visible to other transactions when the autonomous transaction commits. The changes also become visible to the main transaction when it resumes, but only if its isolation level is set to READ COMMITTED (the default). If you set the isolation level of the main transaction to SERIALIZABLE, changes made by its autonomous transactions are not visible to the main transaction when it resumes.
    In the main transaction, rolling back to a savepoint located before the call to the autonomous subprogram does not roll back the autonomous transaction. 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. Oracle raises an exception in the autonomous transaction, which is rolled back if the exception goes unhandled.
    If you try to exit an active autonomous transaction without committing or rolling back, Oracle raises an exception. If the exception goes unhandled, or if the transaction ends because of some other unhandled exception, the transaction is rolled back.

    example
    CREATE OR REPLACE PACKAGE emp_actions AS  -- package specification
       FUNCTION raise_salary (emp_id NUMBER, sal_raise NUMBER) RETURN NUMBER;
    END emp_actions;
    /
    CREATE OR REPLACE PACKAGE BODY emp_actions AS  -- package body
    -- code for function raise_salary
       FUNCTION raise_salary (emp_id NUMBER, sal_raise NUMBER) RETURN NUMBER IS
         PRAGMA AUTONOMOUS_TRANSACTION;
         new_sal NUMBER(8,2);
       BEGIN
         UPDATE employees SET salary = salary + sal_raise WHERE employee_id = emp_id;
         COMMIT;
         SELECT salary INTO new_sal FROM employees WHERE employee_id = emp_id;
         RETURN new_sal;
       END raise_salary;
    END emp_actions;
    /
    



No comments:

Post a Comment