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.

626 ❘ ChaPTer 23 system.trAnsActiOns<br />

isolaTion leVel dirTy reads nonrePeaTable reads PhanTom reads<br />

Read Uncommitted Y Y Y<br />

Read Committed N Y Y<br />

Repeatable Read N N Y<br />

Serializable N N N<br />

The following code segment shows how the isolation level can be set with the TransactionScope<br />

class. With the constructor of TransactionScope, you can set the TransactionScopeOption that was<br />

discussed earlier <strong>and</strong> the TransactionOptions. The TransactionOptions class allows you to define the<br />

IsolationLevel <strong>and</strong> the Timeout:<br />

var options = new TransactionOptions();<br />

options.IsolationLevel = IsolationLevel.ReadUncommitted;<br />

options.Timeout = TimeSpan.FromSeconds(90);<br />

using (TransactionScope scope =<br />

new TransactionScope(TransactionScopeOption.Required,<br />

options))<br />

{<br />

// Read data without waiting for locks from other transactions,<br />

// dirty reads are possible.<br />

}<br />

CusTom resourCe managers<br />

One of the biggest advantages of the new transaction model is that it is relatively easy to create custom<br />

resource managers that participate in the transaction. A resource manager does not manage only durable<br />

resources but can also manage volatile or in-memory resources — for example, a simple int <strong>and</strong> a generic list.<br />

Figure 23-4 shows the relationship between a resource manager <strong>and</strong> transaction classes. The resource<br />

manager implements the interface IEnlistmentNotification that defines the methods Prepare(),<br />

InDoubt(), Commit(), <strong>and</strong> Rollback(). The resource manager implements this interface to manage<br />

transactions for a resource. To be part of a transaction, the resource manager must enlist with the<br />

Transaction class. Volatile resource managers invoke the method EnlistVolatile(); durable resource<br />

managers invoke EnlistDurable(). Depending on the transaction’s outcome, the transaction manager<br />

invokes the methods from the interface IEnlistmentNotification with the resource manager.<br />

TransactionManager<br />

IEnlistmentNotification<br />

Transaction<br />

EnlistVolatile()<br />

EnlistDurable()<br />

figure 23-4<br />

ResourceManager<br />

Prepare()<br />

InDoubt()<br />

Commit()<br />

Rollback()<br />

Resource<br />

The next table explains the methods of the IEnlistmentNotification interface that you must implement<br />

with resource managers. As you review the table, recall the active, prepared, <strong>and</strong> committing phases<br />

explained earlier in this chapter in the “Transaction Phases” section.<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!