Distributed transactions in a Asp.net application

859 Views Asked by At

We have an enterprise application where we are making a call to Database 1, call to a Webservice and then a call to Database 2, all in one sequence of event. We would like to encapsulate the whole processing in a transaction. what is the best way of implementing a distributed transaction in this scenario ?

Environment : SQL 2008, ASP.Net 3.5

1

There are 1 best solutions below

0
On BEST ANSWER

Use a TransactionScope object and different connections (one for each database). The transaction will escalate to a distributed one automatically.

From the example on the MSDN page:

    using (TransactionScope scope = new TransactionScope())
    {
        using (SqlConnection connection1 = new SqlConnection(connectString1))
        {
            // Opening the connection automatically enlists it in the 
            // TransactionScope as a lightweight transaction.
            connection1.Open();

            using (SqlConnection connection2 = new SqlConnection(connectString2))
            {
                // The transaction is escalated to a full distributed
                // transaction when connection2 is opened.
                connection2.Open();
            }
        }

        scope.Complete();
    }