Library code snippets

Transactions made easy with .NET 2.0

One of the more significant improvement in .NET 2.0 is the transactions area. Now with a single line it becomes extremely easy to support transactional code blocks using the concept of “ambient” transaction thanks to TransactionScope in the System.Transactions namespace.

Check out the following code:

using (TransactionScope ts = new TransactionScope()) {

// An "ambient" transaction is placed in the current call context
DbProviderFactory provider;
provider = DbProviderFactories.GetFactory("System.Data.SqlClient");
DbConnection conn = provider.CreateConnection();
conn.ConnectionString = strConn;

// First query. This one will succeed.
DbCommand dbcmd = conn.CreateCommand();
dbcmd.Connection = conn;
dbcmd.CommandText = "DELETE Products";
dbcmd.CommandType = CommandType.Text;

// Second query. This one will fail.
DbCommand dbcmd2 = conn.CreateCommand();
dbcmd2.Connection = conn;
dbcmd2.CommandText = "DELETE INVALIDTABLE";
dbcmd2.CommandType = CommandType.Text;

conn.Open();

try {
// Let's empty the Products table using the query #1.
dbcmd.ExecuteNonQuery();
// The second query will try to empty a non-existent table.
// It will fail and the ts.Complete() method wont be executed.

dbcmd2.ExecuteNonQuery();
//If all the operations succeded,
//then let's commit the transaction.

ts.Complete();

} catch (DbException ex) {

// Error handling block

} finally {

// Cleanup
conn.Close();
ts.Dispose();
}
}

Extremely simple, isn't it? TransactionScope will take care of almost all the transactional stuff in this code block. All that is required to commit the transaction is to call the ts.Complete() method. Notice that the connection object itself is confined within the scope so it automatically participates in the transaction.

You can manipulate the transaction context with Transaction.Current. Please be aware that this is not limited to SQL Server operations. You can create transaction for Oracle, SQL Server data-storages, MSMQ messaging and even bulk copying filesystem operations.

I hope you found this article useful. Happy coding!

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Transactions made easy with .NET 2.0 .

Leave a comment

Sign in or Join us (it's free).

AddThis

Related podcasts

  • A Practical Look at Silverlight 2 Part 1

    Now that Silverlight 2 is at the Olympics and making a big splash, we wanted to explore this fascinating technology more. Microsoft Silverlight 2 is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive ap...

Events coming up

  • Dec 6

    Developing AJAX Web Applications with Castle Monorail

    London, United Kingdom

    Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!