Isolation Level C# Help

At the beginning of this chapter, you saw the ACID properties used to describe transactions. The letter I (Isolation) of ACID is not always fully required. For performance reasons, you might reduce isolation requirements, but you must be aware of the issues that you will encounter if you change the isolation level.

The problems that you can encounter if you don’t completely isolate the scope outside the transaction can be divided into three categories:

¤  Dirty reads – With a dirty read, another transaction can read records that are changed within the transaction. Because the data that is changed within the transaction might roll back to its original state, reading this intermediate state from another transaction is considered “dirty” – the data has no~ been committed. You can avoid this by locking the records to be changed.

¤  Nonrepeatable reads – Nonrepeaiable reads occur when data is read inside a transaction, and while the transaction is running, another transaction changes the same records. 1£ the record is read once more inside the transaction, the result is different – nonrepeatable. You can avoid this by locking the read records.

¤  Phantom reads – Phantom reads happen whena range of data is read, for example, with a WHEREclause. Another transaction can add a new record that belongs to the range that is read within the transaction. A new read with the same WHEREclause returns a different number of . rows. Phantom reads can be a specific problem when doing an UPDATEof a range of rows. For example,UPDATE Addresses SET Zip=4711 WHERE (Zip=2315) updates the ZIP code of all records from 2315 to 4711. After doing the update, there may still be records with a ZIP code of 2315 if another user added a new record with ZIP 2315 while the update was running. You can avoid this by doing a range lock.

When defining the isolation requirements, you can set the isolation level. This is set with an IsolationLevel enumeration that is configured when the transaction is created (either with the constructor of the CommittableTransaction class or with the constructor of the TransactionScope class). The IsolationLevel defines the locking behavior. The next table lists the values of the IsolationLevel enumeration.

The next table gives you a summary of the problems that can Occuras a re’sult of setting, the most commonly used transaction isolation levels.

The following code segment shews how the isolation level can be set with the TransactionScope class. With the constructor of TransactionScope, yeu can set the TransactionScopeOption that was discussed earlier and the TransactionOptions. The TransactionOptions class allows you to. define the IsolationLevel and the Timeout.

The following code segment shews how the isolation level can be set with the TransactionScope class. With the constructor of TransactionScope, yeu can set the TransactionScopeOption that was discussed earlier and the TransactionOptions. The TransactionOptions class allows you to. define the IsolationLevel and the Timeout.

Posted on October 31, 2015 in Transactions

Share the Story

Back to Top