lqxue

常用链接

统计

book

tools

最新评论

Transaction Isolation Levels

Transaction Isolation Levels


Overview

The ANSI/ISO SQL standard defines four levels of transaction isolation in terms of three potential problems that must be prevented between concurrent transactions. These problems are:

  • Dirty Read - A transaction reads data written by another concurrent uncommitted transaction.

  • Non-Repeatable Reads - A transaction re-reads data it has previously read and finds that data has been modified by another transaction (one that has been committed since the transaction's previous read).

  • Phantom Read - A transaction re-executes a query returning a set of rows that satisfy a search condition and finds that the set of rows satisfying the condition has changed due to another recently-committed transaction.

Transaction Isolation Levels

The four levels of transaction isolation are:

  • Uncommitted Read

  • Committed Read

  • Repeatable Read

  • Serializable.

The isolation level that your transaction runs in determines how sensitive your application is to changes other users' transactions make, and consequently, how long your transaction must hold locks to protect against these changes. The ANSI SQL standard defines four levels of transaction isolation.

Uncommitted Read

Uncommitted Read, or dirty read, lets a transaction read any data currently on a data page, regardless of whether or not the data has been committed. For example, another user might have a transaction in progress that has updated data, and that transaction is holding exclusive locks on the data. Your transaction can read the data anyway, and possibly take further actions based on the values you read. The other user might then decide to roll back their transaction, so logically, those changes never occurred.

Committed Read

Committed Read ensures that an operation will never read data another application has changed but not yet committed. Because you can never read uncommitted data, if a transaction running with Committed Read isolation revisits data, that data might have changed, or new rows might appear that meet the criteria of the original query. Rows that appear in this way are called phantoms.

Repeatable Read

If you want the read operations to be repeatable, choose the third isolation level. The Repeatable Read isolation level adds to the properties of Committed Read by ensuring that if a transaction revisits data or if a query is reissued, the data will not have changed. In other words, issuing the same query twice within a transaction will not pick up any changes to data values that another user's transaction has made. No other user can modify the data that your transaction visits as long as you have not yet committed or rolled back your transaction.

Serializable

The Serializable isolation level ensures that if a query is reissued, no data will have changed and no new rows will appear in the interim. In other words, you will not see phantoms if the same query is issued twice within a transaction.

Transaction isolation Levels Behavior

The four transaction isolation levels and the corresponding behaviors are described below:

Isolation Level

Dirty Read

Non-Repeatable Read

Phantom Read

Read Uncommitted

Possible

Possible

Possible

Read Committed

Not possible

Possible

Possible

Repeatable Read

Not possible

Not possible

Possible

Serializable

Not possible

Not possible

Not possible



Support for Transaction Isolation Levels in Orbix E2A Application Server

With Orbix E2A Application Server, you can specify the transaction isolation level for an entire EJB in an application's cc.xml file by setting the new <connection-tx-isolation-level> element in the relevant <resource-ref> and <cmp-datasource> elements.

The valid values for the <connection-tx-isolation-level> element are:

  • TRANSACTION_READ_UNCOMMITTED

  • TRANSACTION_READ_COMMITTED

  • TRANSACTION_REPEATABLE_READ

  • TRANSACTION_SERIALIZABLE

The following is an extract of a cc.xml file using transaction isolation levels for the Account and Person demos that ship with Orbix E2A Application Server.

 

<configuration>
            <enterprise-beans>
            <entity>
            <ejb-name>Account</ejb-name>
            <jndi-name>iona/ipas/simple/Account</jndi-name>
            <jndi-source-name>CosNaming</jndi-source-name>
            <resource-ref>
            <res-ref-name>jdbc/Accounts</res-ref-name>
            <res-ref-link>JDBCAccounts</res-ref-link>
            <connection-tx-isolation-level>TRANSACTION_READ_COMMITTED
            </connection-tx-isolation-level>
            </resource-ref>
            </entity>
            <entity>
            <ejb-name>Person</ejb-name>
            <jndi-name>iona/ipas/simple/Person</jndi-name>
            <jndi-source-name>CosNaming</jndi-source-name>
            <cmp-datasource>
            <res-ref-name>jdbc/Person</res-ref-name>
            <res-ref-link>JDBCPerson</res-ref-link>
            <connection-tx-isolation-level>TRANSACTION_SERIALIZABLE
            </connection-tx-isolation-level>
            </cmp-datasource>
            ...
            </entity>
            </enterprise-beans>
            <resources>
            <resource>
            <resource-name>JDBCPerson</resource-name>
            <jndi-name>iona:cloudscape/demos</jndi-name>
            </resource>
            <resource>
            <resource-name>JDBCAccounts</resource-name>
            


 

     <jndi-name>iona:cloudscape/demos</jndi-name>
            </resource>
            </resources>
            ...
            </configuration>
            

 



         What are Transactions?

A transaction is unit of work containing activities that need to be completed together. For instance, for an online travel site, you might have a session bean method bookTravel that takes a credit card, ensures its valid, charges the price of the ticket on the credit card, and books the ticket. This method invoke methods on other EBJs to help accomplish these steps. For instance, methods of Customer, CreditCard and AirlineTicket beans will be invoked during the execution of this method. If all these steps complete successfully, the transaction succeeds. If any of these steps fails, the entire transaction fails and any changes made by previous steps are rolled back. For instance, if the ticket price is charged on the credit card, but booking the ticket fails, the charge on the credit card must be undone.

If all activities that make up the transaction execute successfully, all changes that were made as part of the transaction are committed. If the transaction fails, these changes are rolled back.

Reliable transactions have the following properties:


http://www.iona.com/support/docs/e2a/asp/5.0/j2ee/DevelopGuide/html/Jta-Bas5.html

posted on 2007-06-29 15:59 lqx 阅读(224) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: