Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
40
Добавлен:
16.04.2013
Размер:
4.96 Mб
Скачать

Autonomous Transactions

Scenario 3

There are insufficient funds to cover the withdrawal, the customer does not have overdraft protection, and the bank therefore withholds the requested funds. This is illustrated by Figure 5–9.

Figure 5–9 Example: Bank Withdrawal—Insufficient Funds WITHOUT Overdraft

Protection

MTx discovers that there are insufficient funds to cover the withdrawal. It finds that the customer does not have overdraft protection and sets a flag to the appropriate value.

Tx2.1, updates the audit table.

MTx Scope rolls back MTx, denying the release of funds. MT Scope ends.

MT Scope

 

 

AT Scope 1

 

 

AT Scope 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MTx

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tx1.1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MTx

Tx2.1

MTx

Defining Autonomous Transactions

Note: This section is provided here to round out your general understanding of autonomous transactions. For a more thorough understanding of autonomous transactions, see PL/SQL User's Guide and Reference.

5-36 Oracle Database Application Developer's Guide - Fundamentals

Autonomous Transactions

To define autonomous transactions, you use the pragma (compiler directive) AUTONOMOUS_TRANSACTION. The pragma instructs the PL/SQL compiler to mark the procedure, function, or PL/SQL block as autonomous (independent).

You can code the pragma anywhere in the declarative section of a procedure, function, or PL/SQL block. But, for readability, code the pragma at the top of the section. The syntax follows:

PRAGMA AUTONOMOUS_TRANSACTION;

In the following example, you mark a packaged function as autonomous:

CREATE OR REPLACE PACKAGE Banking AS

FUNCTION Balance (Acct_id INTEGER) RETURN REAL; -- add additional functions and packages

END Banking;

CREATE OR REPLACE PACKAGE BODY Banking AS

FUNCTION Balance (Acct_id INTEGER) RETURN REAL IS PRAGMA AUTONOMOUS_TRANSACTION;

My_bal REAL; BEGIN

--add appropriate code END;

-- add additional functions and packages...

END Banking;

Restrictions on Autonomous Transactions

Note the following restrictions on autonomous transactions.

You cannot use the pragma to mark all subprograms in a package (or all methods in an object type) as autonomous. Only individual routines can be marked autonomous. For example, the following pragma is illegal:

CREATE OR REPLACE PACKAGE Banking AS

PRAGMA AUTONOMOUS_TRANSACTION; -- illegal FUNCTION Balance (Acct_id INTEGER) RETURN REAL; END Banking;

You cannot execute a PIPE ROW statement in your autonomous routine while your autonomous transaction is open. You must close the autonomous transaction before executing the PIPE ROW statement. This is normally

How Oracle Database Processes SQL Statements 5-37

Соседние файлы в папке Oracle 10g