2011-02-25 51 views
0

我们有一个企业应用程序,我们正在调用数据库1,调用Webservice,然后调用数据库2,所有这些都按照事件的顺序进行。我们希望将整个处理过程封装在一个事务中。在这种情况下实现分布式事务的最佳方式是什么?Asp.net应用程序中的分布式事务

环境:SQL 2008,ASP.Net 3.5

回答

2

使用一个TransactionScope对象和不同的连接(每个数据库)。交易将自动升级为分布式交易。

从MSDN页面上的例子:

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