データベース 1 への呼び出し、Web サービスへの呼び出し、データベース 2 への呼び出しをすべて 1 つのイベント シーケンスで行うエンタープライズ アプリケーションがあります。処理全体をトランザクションにカプセル化したいと考えています。このシナリオで分散トランザクションを実装する最良の方法は何ですか?
環境: SQL 2008、ASP.Net 3.5
データベース 1 への呼び出し、Web サービスへの呼び出し、データベース 2 への呼び出しをすべて 1 つのイベント シーケンスで行うエンタープライズ アプリケーションがあります。処理全体をトランザクションにカプセル化したいと考えています。このシナリオで分散トランザクションを実装する最良の方法は何ですか?
環境: SQL 2008、ASP.Net 3.5
TransactionScope
オブジェクトと異なる接続 (データベースごとに 1 つ) を使用します。トランザクションは自動的に分散トランザクションにエスカレートします。
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();
}