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 ❘ 615<br />

The console application is now changed in that a second student is added by using the same transaction<br />

object tx. Because every AddStudent() method opens a new connection, two connections are associated<br />

with the transaction after the second student is added:<br />

static void TransactionPromotion()<br />

{<br />

var tx = new CommittableTransaction();<br />

Utilities.DisplayTransactionInformation("TX created",<br />

tx.TransactionInformation);<br />

try<br />

{<br />

var s1 = new Student<br />

{<br />

FirstName = "Jörg",<br />

LastName = "Neumann",<br />

Company = "thinktecture"<br />

};<br />

var db = new StudentData();<br />

db.AddStudent(s1, tx);<br />

var s2 = new Student<br />

{<br />

FirstName = "Richard",<br />

LastName = "Blewett",<br />

Company = "thinktecture"<br />

};<br />

db.AddStudent(s2, tx);<br />

Utilities.DisplayTransactionInformation("2nd connection enlisted",<br />

tx.TransactionInformation);<br />

if (Utilities.AbortTx())<br />

{<br />

throw new ApplicationException("transaction abort");<br />

}<br />

tx.Commit();<br />

}<br />

catch (Exception ex)<br />

{<br />

Console.WriteLine(ex.Message);<br />

Console.WriteLine();<br />

tx.Rollback();<br />

}<br />

}<br />

Utilities.DisplayTransactionInformation("TX finished",<br />

tx.TransactionInformation);<br />

code snippet TransactionSamples/Program.cs<br />

Running the application now, you can see that with the first student added the distributed identifier is 0, but<br />

with the second student added the transaction was promoted, so a distributed identifier is associated with<br />

the transaction:<br />

TX created<br />

Creation Time: 7:56:24 PM<br />

Status: Active<br />

Local ID: 0d2f5ada-32aa-40eb-b9d7-cc6aa9a2a554:1<br />

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

2nd connection enlisted<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!