15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

isolation level ❘ 625<br />

The problems that you can encounter if you don’t completely isolate the scope outside the transaction can be<br />

divided into three categories:<br />

➤<br />

➤<br />

➤<br />

Dirty reads — Another transaction can read records that are changed within the transaction. Because<br />

the data that is changed within the transaction might roll back to its original state, reading this<br />

intermediate state from another transaction is considered “dirty” — the data has not been committed.<br />

You can avoid this by locking the records to be changed.<br />

Nonrepeatable reads — When data is read inside a transaction, <strong>and</strong> while the transaction is running,<br />

another transaction changes the same records. If the record is read once more inside the transaction,<br />

the result is different — nonrepeatable. You can avoid this by locking the read records.<br />

Phantom reads — When a range of data is read, for example, with a WHERE clause. Another<br />

transaction can add a new record that belongs to the range that is read within the transaction. A<br />

new read with the same WHERE clause returns a different number of rows. Phantom reads can be a<br />

specific problem when doing an UPDATE of a range of rows. For example, UPDATE Addresses SET<br />

Zip=4711 WHERE (Zip=2315) updates the ZIP code of all records from 2315 to 4711. After doing<br />

the update, there may still be records with a ZIP code of 2315 if another user added a new record with<br />

ZIP 2315 while the update was running. You can avoid this by doing a range lock.<br />

When defining the isolation requirements, you can set the isolation level. This is set with an<br />

IsolationLevel enumeration that is configured when the transaction is created (either with the constructor<br />

of the CommittableTransaction class or with the constructor of the TransactionScope class). The<br />

IsolationLevel defines the locking behavior. The next table lists the values of the IsolationLevel<br />

enumeration.<br />

isolaTion leVel<br />

ReadUncommitted<br />

ReadCommitted<br />

RepeatableRead<br />

Serializable<br />

Snapshot<br />

Unspecified<br />

Chaos<br />

desCriPTion<br />

With ReadUncommitted, transactions are not isolated from each other. With this level,<br />

there is no wait for locked records from other transactions. This way, uncommitted<br />

data can be read from other transactions — dirty reads. This level is usually used<br />

just for reading records where it does not matter if you read interim changes<br />

(for example, reports).<br />

ReadCommitted waits for records with a write-lock from other transactions. This way, a<br />

dirty read cannot happen. This level sets a read-lock for the current record read<br />

<strong>and</strong> a write-lock for the records being written until the transaction is completed.<br />

Reading a sequence of records, with every new record that is read, the prior record is<br />

unlocked. That’s why nonrepeatable reads can happen.<br />

RepeatableRead holds the lock for the records read until the transaction is<br />

completed. This way, the problem of nonrepeatable reads is avoided. Phantom reads<br />

can still occur.<br />

Serializable holds a range lock. While the transaction is running, it is not possible to<br />

add a new record that belongs to the same range from which the data is being read.<br />

The isolation level Snapshot is possible only with SQL Server 2005 <strong>and</strong> later versions.<br />

This level reduces the locks as modified rows are copied. This way, other transactions<br />

can still read the old data without the need to wait for an unlock.<br />

The level Unspecified indicates that the provider is using an isolation level value that<br />

is different from the values defined by the IsolationLevel enumeration.<br />

The level Chaos is similar to ReadUncommitted, but in addition to performing the<br />

actions of the ReadUncommitted value, Chaos does not lock updated records.<br />

The next table gives you a summary of the problems that can occur as a result of setting the most commonly<br />

used transaction isolation levels.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!