Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1




|Views: 16,656|Likes:
Published by sbukka

More info:

Published by: sbukka on Jul 28, 2008
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Before the release of PL/SQL 8.1, each Oracle session could have at most oneactive transaction at a given time. In other words,any and all changes made in your session had to be either saved or erased in their entirety. This restriction has long beenconsidered a drawback in the PL/SQL world. Developers have requested the ability toexecute and save or cancel certain DML statements (INSERT, UPDATE, DELETE)without affecting the overall session’s transaction.You can now accomplish this goal with the autonomous transaction feature of PL/SQL 8.1 and above. When you define a PL/SQL block (anonymous block, procedure,function, packaged procedure, packaged function, database trigger) as an autonomoustransaction, 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.Within the autonomous transaction block, the main transaction is suspended. You performyour SQL operations, commit or roll back those operations, and resume the maintransaction.To define a PL/SQL block as an autonomous transaction you simply include thefollowing statement in your declaration section:
The pragma instructs the PL/SQL compiler to establish a PL/SQL block as autonomousor independent. For the purposes of the autonomous transaction, a PL/SQL block can beany of the following:
Top-level (but not nested) anonymous PL/SQL blocks
Functions and procedures, defined either in a package or as standalone programs
Methods (functions and procedures) of a SQL object type
Database triggers1
You can put the autonomous transaction pragma anywhere in the declaration section of your PL/SQL block. The best option, however, would be to place it before any datastructure declarations. That way, anyone reading your code will immediately identify the program as an autonomous transaction.This pragma is the only syntax change made to PL/SQL to support autonomoustransactions. COMMIT, ROLLBACK, the DML statements—all the rest is as it was before. However, these statements have a different scope of impact and visibility whenexecuted within an autonomous transaction, and you will have to include a COMMIT or ROLLBACK in your program.
The AUTONOMOUS_TRANSACTION pragma instructs the PL/SQL compiler to mark a routine as autonomous (independent).
An autonomous transaction is anindependent transaction started by another transaction, the main transaction .Autonomous transactions let you suspend the main transaction, do SQL operations,commit or roll back those operations, then resume the main transaction.
Usage NotesIn this context, the term routine includes
Top-level (not nested) anonymous PL/SQL blocks
Local, standalone, and packaged functions and procedures
Methods of a SQL object type
Database triggersYou cannot use the pragma to mark all subprograms in a package (or all methods in anobject type) as autonomous. Only individual routines can be marked autonomous. Youcan code the pragma anywhere in the declarative section of a routine. But, 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. So, 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 controlstatements such as COMMIT and ROLLBACK. Also, unlike regular triggers,autonomous triggers can execute DDL statements (such as CREATE and DROP)using native dynamic SQL.
Changes made by an autonomous transaction become visible to other transactions whenthe autonomous transaction commits. The changes also become visible to the maintransaction when it resumes, but only if its isolation level is set to READ COMMITTED(the default).2
If you set the isolation level of the main transaction to SERIALIZABLE, as follows,changes made by its autonomous transactions are not visible to the main transaction whenit resumes:When in the main transaction, rolling back to a savepoint marked before you started anautonomous transaction 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. In thatcase, Oracle raises an exception in the autonomous transaction, which is rolled back if theexception 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, the transaction is rolled back.Related TopicsEXCEPTION_INIT Pragma, RESTRICT_REFERENCES Pragma,SERIALLY_RESUABLE Pragma
EXAMPLE – Autonomous Transaction
In the following example, you mark a packaged function as autonomous:CREATE PACKAGE banking AS...FUNCTION balance (acct_id INTEGER) RETURN REAL;END banking;CREATE PACKAGE BODY banking AS...FUNCTION balance (acct_id INTEGER) RETURN REALISPRAGMA AUTONOMOUS_TRANSACTION;my_bal REAL;BEGIN...END;END banking;In the example below, you mark a database trigger as autonomous. Unlike regular triggers, autonomous triggers can contain transaction control statements.3

Activity (86)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Mert Iyidogan liked this
Füzes László liked this
Gopal Pal liked this
Prasanna Bandla liked this
arunkenya liked this
Kather Basha G liked this
Victor Monroy liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->