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

Using Serializable Transactions for Concurrency Control

Setting the Isolation Level of a Transaction

You can change the isolation level of a transaction using the ISOLATION LEVEL clause of the SET TRANSACTION command, which must be the first command issued in a transaction.

Use the ALTER SESSION command to set the transaction isolation level on a session-wide basis.

See Also: Oracle Database Reference for the complete syntax of the

SET TRANSACTION and ALTER SESSION commands

The INITRANS Parameter

Oracle Database stores control information in each data block to manage access by concurrent transactions. Therefore, if you set the transaction isolation level to

SERIALIZABLE, then you must use the ALTER TABLE command to set INITRANS to at least 3. This parameter causes Oracle Database to allocate sufficient storage in each block to record the history of recent transactions that accessed the block.

Higher values should be used for tables that will undergo many transactions updating the same blocks.

Referential Integrity and Serializable Transactions

Because Oracle Database does not use read locks, even in SERIALIZABLE transactions, data read by one transaction can be overwritten by another. Transactions that perform database consistency checks at the application level should not assume that the data they read will not change during the execution of the transaction (even though such changes are not visible to the transaction). Database inconsistencies can result unless such application-level consistency checks are coded carefully, even when using SERIALIZABLE transactions. Note, however, that the examples shown in this section are applicable for both READ COMMITTED and SERIALIZABLE transactions.

Figure 5–3 on page 5-24 shows two different transactions that perform application-level checks to maintain the referential integrity parent/child relationship between two tables. One transaction checks that a row with a specific primary key value exists in the parent table before inserting corresponding child rows. The other transaction checks to see that no corresponding detail rows exist before deleting a parent row. In this case, both transactions assume (but do not ensure) that data they read will not change before the transaction completes.

How Oracle Database Processes SQL Statements 5-23

Using Serializable Transactions for Concurrency Control

Figure 5–3 Referential Integrity Check

B's query does

 

 

 

 

not prevent this

 

 

 

 

insert

 

TRANSACTION B

 

 

 

TRANSACTION A

 

 

 

 

 

 

read parent (it exists)

read child rows (not found)

 

 

 

insert child row(s)

delete parent

 

 

 

 

 

commit work

commit work

 

 

 

 

 

 

 

 

A's query does not prevent this delete

The read issued by transaction A does not prevent transaction B from deleting the parent row, and transaction B's query for child rows does not prevent transaction A from inserting child rows. This scenario leaves a child row in the database with no corresponding parent row. This result occurs even if both A and B are SERIALIZABLE transactions, because neither transaction prevents the other from making changes in the data it reads to check consistency.

As this example shows, sometimes you must take steps to ensure that the data read by one transaction is not concurrently written by another. This requires a greater degree of transaction isolation than defined by SQL92 SERIALIZABLE mode.

Using SELECT FOR UPDATE

Fortunately, it is straightforward in Oracle Database to prevent the anomaly described:

Transaction A can use SELECT FOR UPDATE to query and lock the parent row and thereby prevent transaction B from deleting the row.

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

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