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();
}
精彩评论