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.

618 ❘ ChaPTer 23 system.trAnsActiOns<br />

With the output of the application, you can see the root transaction with its identifier. Because of the option<br />

DependentCloneOption.BlockCommitUntilComplete, the root transaction waits in the Commit() method<br />

until the outcome of the dependent transaction is defined. As soon as the dependent transaction is finished,<br />

the transaction is committed:<br />

Root TX created<br />

Creation Time: 8:35:25 PM<br />

Status: Active<br />

Local ID: 50126e07-cd28-4e0f-a21f-a81a8e14a1a8:1<br />

Distributed ID: 00000000-0000-0000-0000-0000000000<br />

Abort the Transaction (y/n) n<br />

Dependent Transaction<br />

Creation Time: 8:35:25 PM<br />

Status: Active<br />

Local ID: 50126e07-cd28-4e0f-a21f-a81a8e14a1a8:1<br />

Distributed ID: 00000000-0000-0000-0000-0000000000<br />

Dependent TX Complete<br />

Root TX finished<br />

Creation Time: 8:35:25 PM<br />

Status: Committed<br />

Local ID: 50126e07-cd28-4e0f-a21f-a81a8e14a1a8:1<br />

Distributed ID: 00000000-0000-0000-0000-0000000000<br />

Creation Time: 8:35:25 PM<br />

Status: Committed<br />

Local ID: 50126e07-cd28-4e0f-a21f-a81a8e14a1a8:1<br />

Distributed ID: 00000000-0000-0000-0000-0000000000<br />

ambient Transactions<br />

The really big advantage of System.Transactions is the ambient transactions feature. With ambient<br />

transactions, there is no need to manually enlist a connection with a transaction; this is done automatically<br />

from the resources supporting ambient transactions.<br />

An ambient transaction is associated with the current thread. You can get <strong>and</strong> set the ambient transaction<br />

with the static property Transaction.Current. APIs supporting ambient transactions check this property<br />

to get an ambient transaction <strong>and</strong> enlist with the transaction. ADO.<strong>NET</strong> connections support ambient<br />

transactions.<br />

You can create a CommittableTransaction object <strong>and</strong> assign it to the property Transaction.<br />

Current to initialize the ambient transaction. Another way to create ambient transactions is with the<br />

TransactionScope class. The constructor of the TransactionScope creates an ambient transaction.<br />

Important methods of the TransactionScope are the Complete() <strong>and</strong> the Dispose() methods. The<br />

Complete() method sets the happy bit for the scope, <strong>and</strong> the Dispose() method finishes the scope <strong>and</strong><br />

commits or rolls back the transaction if the scope is a root scope.<br />

Because the TransactionScope class implements the IDisposable interface, you can define the scope<br />

with the using statement. The default constructor creates a new transaction. Immediately after creating<br />

the TransactionScope instance, the transaction is accessed with the get accessor of the property<br />

Transaction.Current to display the transaction information on the console.<br />

To get the information when the transaction is completed, the method OnTransactionCompleted() is set<br />

to the TransactionCompleted event of the ambient transaction.<br />

Then a new Student object is created <strong>and</strong> written to the database by calling the StudentData.<br />

AddStudent() method. With ambient transactions, it is no longer necessary to pass a Transaction object<br />

to this method because the SqlConnection class supports ambient transactions <strong>and</strong> automatically enlists<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!