开发者

Distributed transactions in a Asp.net application

开发者 https://www.devze.com 2023-02-13 14:26 出处:网络
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 pro

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 transact开发者_开发百科ion. what is the best way of implementing a distributed transaction in this scenario ?

Environment : SQL 2008, ASP.Net 3.5


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();
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号