Professional Documents
Culture Documents
Syntax
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 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.
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 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.
Pros Cons
Allows you to perform DML from a Deadlocks may occur more frequently as a
SELECT single user can now deadlock themselves
Allows for more modular code with less Must be used in a top level anonymous
side effects (avoids the "hey – you rolled block, procedure, or function. Cannot be
back my work!") included in a nested PL/SQL block.
EXAMPLES:
DECLARE
PRAGMA AUTONOMOUS_TRANSACTION;
emp_id NUMBER(6);
amount NUMBER(6,2);
BEGIN
emp_id := 200;
amount := 200;
UPDATE employees SET salary = salary - amount WHERE
employee_id = emp_id;
COMMIT;
END;
/