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.

system.Transactions ❘ 611<br />

}<br />

}<br />

}<br />

}<br />

finally<br />

{<br />

connection.Close();<br />

}<br />

A big advantage of creating transactions with System.EnterpriseServices is that multiple objects can<br />

easily run within the same transaction, <strong>and</strong> transactions are automatically enlisted. The disadvantages are<br />

that it requires the COM+ hosting model, <strong>and</strong> the class using the features of this technology must be derived<br />

from the base class ServicedComponent .<br />

Enterprise Services <strong>and</strong> using COM+ transactional services are covered in the<br />

downloadable Chapter 51, “Enterprise Services.”<br />

sysTem.TransaCTions<br />

The namespace System.Transactions became available with .<strong>NET</strong> 2.0 <strong>and</strong> brings a modern transaction<br />

programming model to .<strong>NET</strong> applications.<br />

This namespace offers a few dependent TransactionXXX classes. Transaction is the base class of all<br />

transaction classes <strong>and</strong> defi nes properties, methods, <strong>and</strong> events available with all transaction classes.<br />

CommittableTransaction is the only transaction class that supports committing. This class has a<br />

Commit() method; all other transaction classes can do only a rollback. The class DependentTransaction is<br />

used with transactions that are dependent on another transaction. A dependent transaction can depend on a<br />

transaction created from the committable transaction. Then the dependent transaction adds to the outcome<br />

of the committable transaction whether or not it is successful. The class SubordinateTransaction is used<br />

in conjunction with the Distributed Transaction Coordinator (DTC). This class represents a transaction that<br />

is not a root transaction but can be managed by the DTC.<br />

The following table describes the properties <strong>and</strong> methods of the Transaction class.<br />

TransaCTion Class member<br />

Current<br />

IsolationLevel<br />

TransactionInformation<br />

EnlistVolatile()<br />

EnlistDurable()<br />

EnlistPromotableSinglePhase()<br />

Rollback()<br />

desCriPTion<br />

The property Current is a static property without the need to have<br />

an instance. Transaction.Current returns an ambient transaction<br />

if one exists. Ambient transactions are discussed later in this chapter.<br />

The IsolationLevel property returns an object of type<br />

IsolationLevel. IsolationLevel is an enumeration that defi nes<br />

what access other transactions have to the interim results of the<br />

transaction. This affects the I in ACID; not all transactions are isolated.<br />

The TransactionInformation property returns a<br />

TransactionInformation object. TransactionInformation<br />

gives you information about the current state of the transaction, the<br />

time when the transaction was created, <strong>and</strong> transaction identifi ers.<br />

With the enlist methods EnlistVolatile(), EnlistDurable(),<br />

<strong>and</strong> EnlistPromotableSinglePhase(), you can enlist custom<br />

resource managers that participate with the transaction.<br />

With the Rollback() method, you can abort a transaction <strong>and</strong> undo<br />

everything to set all results to the state before the transaction.<br />

continues<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!