skip to Main Content

System.Transactions: New and Improved Transaction Management Model for .NET 2.0

Ok, so System.Transactions is not new to many .NET developers but it’s new to me. We have just started to research transaction management in my current .NET 2.0 project at work and  System.Transactions is looking beautiful. It gives us exactly what we need without the overhead of the old EnterpriseServices model (registering with COM+, having a strong name key, etc.) or the high maintenance of manual transaction management.

System.Transactions is more light-weight compared to EnterpriseServices. The programming model is relatively simple. You use the TransactionScope class to wrap your code inside a transaction. TransactionScope can be nested and can be assigned different transaction options such as Required or RequiresNew, similar to the various transaction attributes for EnterpriseServices.

using (TransactionScope scope = new TransactionScope()) 
{ 
  // do some work here (like executing a SQL, calling a method, etc.) 

  // The Complete method commits the transaction. If an exception has been thrown, 
  // Complete is not  called and the transaction is rolled back. 
  scope.Complete(); 
}

In my brief testing with Enterprise Library 2.0 and an Oracle database via Oracle Data Provider, I found that System.Transactions always enlist my transactions in MSDTC (Microsoft Distributed Transaction Coordinator). I am sure there is some overhead associated with this. I will do some more performance test later to find out exactly what the overhead is. I was hoping that for a transaction involing a single database/connection string, that MSDTC would not be needed. But further research indicated that the non-MSDTC/light-weight transaction only works with SQL Server 2005.

If your transaction management needs extend beyond databases, you can even write your own resource manages so that operations such as copying a file can be wrapped inside a transaction as well. Look into the IEnlistmentNotification interface.

Here are some good articles about System.Transactions for your further reading:

StringBuilder is not always faster – Part 1 of 2

How often have you been told to use StringBuilder to concatenate strings in .NET? My guess is often enough. Here is something you may not know about string concatenation: StringBuilder is not always faster. There are already many articles out there that explain the why’s, I am not going to do that here. But I do have some test data for you.

When concatenating three values or less, traditional concatenation is faster (by a very small margin)

This block of code took 1484 milliseconds to run on my PC:

for (int i = 0; i <= 1000000; i++) 
{ 
    // Concat strings 3 times using StringBuilder 
    StringBuilder s = new StringBuilder(); 
    s.Append(i.ToString()); 
    s.Append(i.ToString()); 
    s.Append(i.ToString()); 
}

And this one, using traditional concatenation, took slightly less time (1344 milliseconds):

for (int i = 0; i <= 1000000; i++) 
{ 
    // Concat strings 3 times using traditional concatenation 
    string s = i.ToString(); 
    s = s + i.ToString(); 
    s = s + i.ToString(); 
}

The above data suggests that StringBuilder only starts to work faster once the number of concatenations exceed 3.

Building strings from literals

When building a large string from several string literals (such as building a SQL block, or a client side javascript block), use neither traditional concatenation nor StringBuilder. Instead, choose one of the methods below:

+ operator

// Build script block 
string s = "<script>" 
       + "function test() {" 
       + "  alert('this is a test');" 
       + "  return 0;" 
       + "}";

The compiler concatenates that at compile time. At run-time, that works as fast as a big string literal.

@ string literal

I sometimes use the @ string literal which allows for newlines (I find this syntax is harder to maintain, formatting-wise, than using the + operator):

string s = @"<script> 
        function test() { 
        alert('this is a test'); 
        return 0; 
        }";

Both methods above run about 40 times faster than using StringBuilder or traditional string concatenation.

Rules of Thumb

  • When concatenating three dynamic string values or less, use traditional string concatenation.
  • When concatenating more than three dynamic string values, use StringBuilder.
  • When building a big string from several string literals, use either the @ string literal or the inline + operator.

Updated 2007-09-29

I have posted a follow-up article to provide more detailed analysis and to answer some of the questions asked by readers.

Google Maps on Your Windows Mobile Phone

Google has just made available Google Maps for Windows Mobile. Here’s the blurb on Google Maps Mobile Page:

Take the power of Google Maps with you on your mobile phone.

Real-time traffic — See where the congestion is, and estimate delays in over 30 major US metropolitan areas.

Detailed directions — Whether you plan to walk or drive, your route is displayed on the map itself, together with step-by-step directions.

Integrated search results — Local business locations and contact information appear all in one place, integrated on your map.

Easily movable maps — Interactive maps let you zoom in or out, and move in all directions so you can orient yourself visually.

Satellite imagery — Get a bird’s eye view of your desired location. (It’s like you’re there, we swear.

I tried the app on my Samsung i730 Windows Mobile phone and it worked great. Map data is retrieved from Google’s servers as needed just like regular Google Maps. Response time is slower than on a desktop but still very usable on my Verizon EDVO connection.

If you are a user of Google Maps then the screen below will look familiar to you:

Google Maps

Note the transparent buttons. Great idea.

Searching for local businesses is a snap. Click Search . Type in the name of the busines. Hit OK. Search matches are displayed on the map as pushpins:

Google Maps

There is also the Satellite View, but missing is the Hybrid View.

Google Maps

More info here.

Google Co-op

Google Co-op allows you to create customized search engines to fit your search interests. .NET developers, you may want to give the following pre-customized search engines a try:

For me, Co-op is a good idea but for now I will stick with just regular Google, at least for .NET related searches. I gave SearchDotNet.com a try and was initially underwhelmed my the number of matches. For example, searching for “EntryAssembly” using the DotNetSearch engine above yielded two pages of matches. Regular Google returns 387 pages.

Back To Top