2011-02-25 92 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(); 
    }