Hello,

My name is Nate Moch and I am a Software Design Engineer in Test at Microsoft and I work on the Transactions team.  Specifically, I have been working on the System.Transactions namespace that is part of Visual Studio 2005.  I can’t describe how excited I am about this new feature and the way it will change how you use Transactions!  System.Transactions makes it easier and faster than ever to incorporate Transactions into your applications.  I am blogging because I want to answer questions and spread the word on how to take advantage of all the cool features we have created!  If you would like to see some code snippets, have any questions, or would just like to share some of your opinions on System.Transactions, please post a comment, or send me an email at natemoch@microsoft.com.

For my first entry, here is a little introduction to System.Transactions.

Overview of System.Transactions

The System.Transactions infrastructure makes transactional programming simple and efficient by supporting SQL Server, ADO.NET, MSMQ, and the distributed transaction coordinator (DTC). It provides both an explicit programming model based on the Transaction class, as well as an implicit programming model using the TransactionScope class, in which transactions are automatically managed by the infrastructure.

For example, here is all you need to use Transactions with SQL:

try
{
  using (TransactionScope scope = new TransactionScope())
  {
    try
    {
      using (SqlConnection conn = new SqlConnection(connString))
      {
        conn.Open();
        // Build query and execute
      }                             
    }
    catch (Exception e)
    { }
    scope.Complete();
  }
}
catch (TransactionException e)
{ }

 

System.Transactions also provides types for you to implement a resource manager. The transaction manager native to the System.Transactions infrastructure allows volatile resources or a single durable resource enlistment to commit or roll back efficiently.

Here is the new interface used by resource managers to enlist in transactions:

tx.EnlistVolatile(new EnlistmentNotification, EnlistmentOptions.None);

public class EnlistmentNotification : IEnlistmentNotification
{
            Transaction tx = null;
            public EnlistmentNotification (Transaction transaction)
            {
                        this.tx = transaction;
            }
            public void Prepare(PreparingEnlistment preparingEnlistment)
            {
                        preparingEnlistment.Prepared();
            }
            public void Commit(Enlistment enlistment)
            {
                        enlistment.Done();
            }
            public void Rollback(Enlistment enlistment)
            {
                        enlistment.Done();
            }
            public void InDoubt(Enlistment enlistment)
            {
                        enlistment.Done();
            }
}

The transaction manager also transparently escalates local transactions to distributed transactions by coordinating through a disk-based transaction manager like the DTC, when an additional durable resource manager enlists itself with a transaction. There are two key ways that the System.Transactions infrastructure provides enhanced performance.

1.) Dynamic Escalation, which means that the System.Transactions infrastructure only engages the MSDTC when it is actually required for a transaction.

2.) Promotable Enlistments, which allows a resource, such as a database, to take ownership of the transaction if it is the only entity participating in the transaction. Later, if needed, the System.Transactions infrastructure can still escalate the management of the transaction to MSDTC. This further reduces the chance of using the MSDTC.

Please let me know if there is anything you would like to know regarding System.Transactions.