Thursday, March 1, 2007


While developing applications we often need to handle transactions. Before .NET 2 various options were available like ADO.NET Transaction model, Enterprise Services Transactions etc but the new TransactionScope has really made it easy to manage transactions.
With transactions scope we can do transactions across multiple application layers as well as the distributed ones.
The good news is that System.Transactions has the unique capability of knowing whether to use a distributed or lightweight transaction. It will use a lightweight transaction if there is a single domain accessing a single database, or it will use a distributed transaction similar to the enterprise services transaction model if there are multiple databases to access.

To use this new transaction functionality we need to add the namespace System.Transactions.

To use the TransactionScope we can simply add it in a using block like this

using (TransactionScope ts = new TransactionScope())
// code goes here
//and once we are done to complete the transaction


Different options are available with TransactionScope which could be exploited according to needs.

One thing to be taken into consideration is that if applications or components involved in a transaction are not residing on the same machine we need to do some settings else an exception like the given below could be raised.

{"The transaction has already been implicitly or explicitly committed or aborted."}
System.Transactions.TransactionException: {"The transaction has already been implicitly or explicitly committed or aborted."}

To overcome this problem we need to set up MSDTC in both the systems.

Setup both the systems to allow transactions by following these steps

• Open the Control Panel and select Administrative Tools.
• From Administrative Tools select Component Services.
• Open the Component Services tree to display My Computer.
• Right click My Computer and select properties to display the properties dialog.
• In the dialog select the MSDTC tab and click the Security Configuration button.
• In the Security Configuration dialog, make sure Allow Inbound, Allow Outbound and No Authentication Required are selected.

Considering an application running on Win2003 and communicating with SQL server on a machine with Win2k there are not security setting required for Win2k. In that case “No Authentication Required” should be selected on the application server.

For more details on Transactions:

No comments: