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.

610 ❘ ChaPTer 23 system.trAnsActiOns<br />

}<br />

}<br />

}<br />

Trace.WriteLine("Error: " + ex.Message);<br />

tx.Rollback();<br />

}<br />

finally<br />

{<br />

connection.Close();<br />

}<br />

If you have multiple comm<strong>and</strong>s that should run in the same transaction, every comm<strong>and</strong> must be associated<br />

with the transaction. Because the transaction is associated with a connection, every one of these comm<strong>and</strong>s<br />

must also be associated with the same connection instance. ADO.<strong>NET</strong> transactions do not support<br />

transactions across multiple connections; it is always a local transaction associated with one connection.<br />

When you create an object persistence model using multiple objects, for example, classes Course<br />

<strong>and</strong> CourseDate , which should be persisted inside one transaction, it gets very diffi cult using ADO.<strong>NET</strong><br />

transactions. Here, it is necessary to pass the transaction to all the objects participating in the<br />

same transaction.<br />

ADO.<strong>NET</strong> transactions are not distributed transactions. In ADO.<strong>NET</strong> transactions, it<br />

is diffi cult to have multiple objects working on the same transaction.<br />

system.enterpriseservices<br />

With Enterprise Services, you get a lot of services for free. One of them is automatic transactions.<br />

Using transactions with System.EnterpriseServices has the advantage that it is not necessary to deal<br />

with transactions explicitly; transactions are automatically created by the runtime. You just have to add the<br />

attribute [Transaction] with the transactional requirements to the class. The [AutoComplete] attribute<br />

marks the method to automatically set the status bit for the transaction: if the method succeeds, the success<br />

bit is set, so the transaction can commit. If an exception happens, the transaction is aborted:<br />

using System;<br />

using System.Data.SqlClient;<br />

using System.EnterpriseServices;<br />

using System.Diagnostics;<br />

namespace Wrox.ProCSharp.Transactions<br />

{<br />

[Transaction(TransactionOption.Required)]<br />

public class CourseData: ServicedComponent<br />

{<br />

[AutoComplete]<br />

public void AddCourse(Course course)<br />

{<br />

var connection = new SqlConnection(<br />

Properties.Settings.Default.CourseManagementConnectionString);<br />

SqlComm<strong>and</strong> courseComm<strong>and</strong> = connection.CreateComm<strong>and</strong>();<br />

courseComm<strong>and</strong>.Comm<strong>and</strong>Text =<br />

"INSERT INTO Courses (Number, Title) VALUES (@Number, @Title)";<br />

connection.Open();<br />

try<br />

{<br />

courseComm<strong>and</strong>.Parameters.AddWithValue("@Number", course.Number);<br />

courseComm<strong>and</strong>.Parameters.AddWithValue("@Title", course.Title);<br />

courseComm<strong>and</strong>.ExecuteNonQuery();<br />

www.it-ebooks.info

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

Saved successfully!

Ooh no, something went wrong!