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

Autonomous Transactions

Examples of Autonomous Transactions

The two examples in this section illustrate some of the ways you can use autonomous transactions.

As these examples illustrate, there are four possible outcomes that can occur when you use autonomous and main transactions. Table 5–6 presents these possible outcomes. As you can see, there is no dependency between the outcome of an autonomous transaction and that of a main transaction.

Table 5–6 Possible Transaction Outcomes

Autonomous Transaction

Main Transaction

 

 

Commits

Commits

Commits

Rolls back

Rolls back

Commits

Rolls back

Rolls back

 

 

Entering a Buy Order

In this example, illustrated by Figure 5–6, a customer enters a buy order. That customer's information (such as name, address, phone) is committed to a customer information table—even though the sale does not go through.

Figure 5–6 Example: A Buy Order

MT Scope begins the main transaction, MTx inserts the buy order into a table.

MTx invokes the autonomous transaction scope (AT Scope). When AT Scope begins, MT Scope suspends.

ATx, updates the audit table with customer information.

MTx seeks to validate the order, finds that the selected item is unavailable, and therefore rolls back the main transaction.

MT Scope

MTx

AT Scope

ATx

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

Autonomous Transactions

Example: Making a Bank Withdrawal

In this example, a customer tries to make a withdrawal from a bank account. In the process, a main transaction calls one of two autonomous transaction scopes (AT Scope 1, and AT Scope 2).

The following diagrams illustrate three possible scenarios for this transaction.

Scenario 1: There are sufficient funds to cover the withdrawal and therefore the bank releases the funds

Scenario 2: There are insufficient funds to cover the withdrawal, but the customer has overdraft protection. The bank therefore releases the funds.

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.

How Oracle Database Processes SQL Statements 5-33

Autonomous Transactions

Scenario 1

There are sufficient funds to cover the withdrawal and therefore the bank releases the funds. This is illustrated by Figure 5–7.

Figure 5–7 Example: Bank Withdrawal—Sufficient Funds

MTx generates a transaction ID.

Tx1.1 inserts the transaction ID into the audit table and commits.

MTx validates the balance on the account.

Tx2.1, updates the audit table using the transaction ID generated above, then commits.

MTx releases the funds. MT Scope ends.

MT Scope

 

 

AT Scope 1

 

 

AT Scope 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MTx

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tx1.1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MTx

Tx2.1

MTx

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

Autonomous Transactions

Scenario 2

There are insufficient funds to cover the withdrawal, but the customer has overdraft protection. The bank therefore releases the funds. This is illustrated by Figure 5–8.

Figure 5–8 Example: Bank Withdrawal—Insufficient Funds WITH Overdraft Protection

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

Tx2.1, updates the audit table.

MTx, releases the funds. MT Scope ends.

MT Scope

 

 

AT Scope 1

 

 

AT Scope 2

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MTx

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Tx1.1

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

MTx

Tx2.1

MTx

How Oracle Database Processes SQL Statements 5-35

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