http://www.developer.com/db/article.php/3706251/Database-Isolation-Levels.htm
This article is designed to introduce you the concepts of isolation levels—the concepts that every database programmer needs to know to develop applications that allow multi-user access to the database. Data consistency is a term used to describe the accuracy of data. For example, suppose you have a table that maintains a count of vehicles available for sale in car dealerships. When a vehicle is transferred from dealership A to dealership B, the count has to decrease in dealership A and increase in dealership B. Both actions must take place; if one occurs and another does not, the data becomes inaccurate—data inconsistency occurs. Data inconsistency can occur due to several factors: To prevent the occurrence of the above situations, a DBMS (database management system) relies on isolation levels and locks. An isolation levels mechanism is used to isolate each transaction in a multi-user environment. The correct use of the isolation levels mechanism prevents applications from introducing errors that can occur from the following situations: 1. Lost Updates: This situation occurs when two transactions attempt to update the same data. Consider the following example: In the above situation, updates performed by Transaction A are lost. 2. Dirty Reads: This situation occurs when transactions read data that has not been committed. Consider the following example: 3. Nonrepeatable Reads: This situation occurs when a transaction reads the same query multiple times and results are not the same each time. Consider the following example: 4. Phantoms: This situation occurs when a row of data matches the first time but does not match subsequent times. Consider the following example: The above four phenomenona demonstrate that there is a need to utilize a mechanism called ISOLATION LEVELS. The next sections of this article will explain DB2 isolation levels. Most of the rules regarding isolation levels are the same across most transactional databases but some rules may vary; therefore, I advise you consult other sources when dealing with databases other than DB2. A DB2 database supports four isolation levels: This isolation level is the most restrictive of the four. It makes sure that none of the phenomenona will occur. It is also the most restrictive when it comes to database concurrency. (Database concurrency controls ensure that transactions occur in an ordered fashion. The main job of these controls is to protect transactions issued by different users/applications from the effects of each other. They must preserve the four characteristics of database transactions: atomicity, isolation, consistency and durability.) When a transaction with isolation level REAPETABLE READ (RR) executes, it locks every row it references. It is important to understand that the lock is held for every row, not only rows that are updated, but also those that are merely selected. Consider the following situation: A car dealership manager is executing a report that produces a total number of vehicles for sale. During this time, a salesperson is attempting to submit a purchase order that will decrement the number of vehicles on the lot. If the manager's report is running with isolation level RR, the purchase order will not go through because all rows are being locked by the manager's report. The above situation reveals the importance of understanding the implications of using the Repeatable Read isolation level. Now, look at another example but in more technical terms: Read Stability is not as restrictive as the Repeatable Read isolation level; therefore, it does not prevent all the phenomenona. The difference between Read Stability and Repeatable Read is as follows: In Read Stability, only rows that are retrieved or modified are locked, whereas in Repeatable Read, all rows that are being referenced are locked. This crucial difference makes Phantoms possible, but protects against Lost Updates, Dirty Reads, and Nonrepeatable Reads. Look at the following scenario: Cursor Stability protects against Dirty Reads and Lost Updates, but does not protect against Phantoms and Nonrepeatable Reads. Cursor Stability only locks one row at a time—the row that is currently being referenced by a cursor. As soon as the cursor moves to the next row, the lock on the previous row is released. This provides for much more concurrency because other transactions can update rows before and after a row that is being referenced by a cursor with cursor stability. There are two exceptions to this rule: This isolation level is a bit more complicated, so let me go through several scenarios: An uncommitted read is the least restrictive of all isolation levels and provides the most database concurrency. This isolation level is mostly used to retrieve read-only data. Transactions running under this isolation level can see uncommitted data. An Uncommitted Read transaction locks only those rows that it modifies or if another transaction attempts to alter or drop the table the rows are being retrieved from. Uncommitted Read does not protect against dirty reads, nonrepeatable reads, and phantoms, but protects against lost updates. Even though transactions running under Uncommitted Read will see uncommitted rows, they will not see tables, views, and indexes that have been created or dropped until the transaction that created/dropped them commits its changes. Consider the following situations: The isolation level can be set in more than one way: In this article, you have learned about isolation levels, data concurrency, and database phenomena. You have also briefly touched a much broader subject of locks. Aleksey Shevchenko has been working with object-oriented languages for over seven years. For the past four years, he has served as a technical lead and a project manager. Aleksey has been implementing Enterprise IT solutions for Wall Street and the manufacturing and publishing industries.
Database Isolation Levels
October 19, 2007
Introduction
Data Consistency
Isolation Levels
Repeatable Read
Read Stability
Cursor Stability
Uncommited Read
How to Specify the Isolation Level
ISOLATION [RR | RS | CS | UR]
WITH [RR | RS | CS | UR]
Note: If the isolation level is not explicitly set, the default isolation level used is CURSOR STABILITY.
Conclusion
References
About the Author