Open棟梁Project - マイクロソフト系技術情報 Wiki

目次

概要

TransactionScopeクラスは、2相コミットを実現するための.NET 2.0のAPIであり、
内部的には、トランザクション マネージャ(TM)・リソース マネージャ(RM)であるMS-DTCを使用している。

経緯

ASP.NET 1.xでは、MS-DTCEnterprise Services上からの利用に限られたが、Enterprise Servicesを使用した場合、ビジネス プロセス・ビジネス コンポーネントなどのアプリケーション ブロックがEnterprise Services上に切り出す必要があったが、Enterprise Servicesアプリケーション開発は難易度が高く、あまり活用されなかった。このため、ASP.NET 2.0からは、上記のTransactionScopeクラスを利用することで、中間層を用いないで、ASP.NET上から簡単に2相コミットを利用できるように改善が図られた(Windows DNAのASP上からMS-DTCを利用する方式に近い)。

動作確認用のサンプル プログラム(ASP.NET)

2相コミットの動作確認用サンプル プログラムを用いて、TransactionScopeクラスの利用方法を説明する。

SQL Server

下記は、SQL Server用のデータ アクセス ライブラリである、
SqlClient?を利用した場合のサンプル コード。

private void Two_Phase_Commit(bool flag)
{
  TransactionOptions txopt = new TransactionOptions();
  txopt.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
        
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead;
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.Snapshot;        
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
        
  using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, txopt))
  {
    using (SqlConnection con = new SqlConnection(@"Data Source=AAA;Initial Catalog=northwind;User ID=xxx;Password=xxx;"))
    {
      using (SqlCommand com = new SqlCommand())
      {
        com.Connection = con;
        com.CommandText = "insert into table1(bbb) values('データ')";
        try
        {
          con.Open();
          com.ExecuteNonQuery();
        }
        finally
        {
          con.Close();
        }
      }
    }
    using (SqlConnection con = new SqlConnection(@"Data Source=BBB;Initial Catalog=northwind;User ID=xxx;Password=xxx;"))
    {
      using (SqlCommand com = new SqlCommand())
      {
        com.Connection = con;
        com.CommandText = "insert into table1(bbb) values('データ')";
        try
        {
          con.Open();
          com.ExecuteNonQuery();
        }
        finally
        {
          con.Close();
        }
      }
    }

    if (flag) // flag変数はコミット、ロールバックのテストのための実装
    {
      scope.Complete();
    }
    else{ }
  }
}

Oracle

下記は、Oracle用のデータ アクセス ライブラリである、System.Data.OracleClient?、ODP.NETを利用した場合のサンプル コードである。実装を見てわかるとおり、ADO.NETの仕様に合わせ、APIのインターフェイスも共通化されているため、他のDBMSのデータプロバイダも含め、実装に大きな違いは無い。

private void Two_Phase_Commit(bool flag)
{
  TransactionOptions txopt = new TransactionOptions();
  txopt.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;

  //txopt.IsolationLevel = System.Transactions.IsolationLevel.RepeatableRead;
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.Serializable;
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.Snapshot;
  //txopt.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;

  using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, txopt))
  {
    using (OracleConnection con = new OracleConnection(@"User Id=xxx;Password=xxx;Data Source=AAA/orcl;"))
    {
      using (OracleCommand com = new OracleCommand())
      {
        com.Connection = con;
        com.CommandText = "insert into table1(aaa, bbb) values(1, 'データ')";
        try
        {
          con.Open();
          com.ExecuteNonQuery();
        }
        finally
        {
          con.Close();
        }
      }
    }
    using (OracleConnection con = new OracleConnection(@"User Id=xxx;Password=xxx;Data Source=BBB/orcl;"))
    {
      using (OracleCommand com = new OracleCommand())
      {
        com.Connection = con;
        com.CommandText = "insert into table1(aaa, bbb) values(1, 'データ')";
        try
        {
          con.Open();
          com.ExecuteNonQuery();
        }
      finally
      {
        con.Close();
        }
      }
    }

    if (flag) // flag変数はコミット、ロールバックのテストのための実装
    {
      scope.Complete();
    }
    else{ }
  }
}

MS-DTC環境の構築

詳しくはマニュアルなどを参照して下さい。
(コチラを調べてから随分と期間が空いているので、設定方法も変わっていると思われるため)。


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS