7

System.Transactions名前空間を見たことがありますが、実際にこの名前空間を使用してRDMBSを作成できますか?

しかし、いくつかの例を見たとき、System.Transactionsが単純な試行を超えて成功/失敗の結果を得る方法を理解していませんか?

これはMSDNのWebサイトの例です。非常に単純かもしれませんが、このサンプルの利点を理解できません。次のサンプルの単純なtry/catchとトランザクションスコープの違いを教えてもらえますか。

RDBMSを作成する(独自のRDMBSを作成する)ことになっている場合、実行する操作のディスクに大量のログを書き込む必要があることを理解しています。最後に、ロールバックの場合はそれらの操作を元に戻しますが、ここには何もありません。何かを元に戻すことについて。

// This function takes arguments for 2 connection strings and commands to create a transaction 
// involving two SQL Servers. It returns a value > 0 if the transaction is committed, 0 if the 
// transaction is rolled back. To test this code, you can connect to two different databases 
// on the same server by altering the connection string, or to another 3rd party RDBMS by 
// altering the code in the connection2 code block.
static public int CreateTransactionScope(
    string connectString1, string connectString2,
    string commandText1, string commandText2)
{
    // Initialize the return value to zero and create a StringWriter to display results.
    int returnValue = 0;
    System.IO.StringWriter writer = new System.IO.StringWriter();

    try
    {
        // Create the TransactionScope to execute the commands, guaranteeing
        // that both commands can commit or roll back as a single unit of work.
        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();

                // Create the SqlCommand object and execute the first command.
                SqlCommand command1 = new SqlCommand(commandText1, connection1);
                returnValue = command1.ExecuteNonQuery();
                writer.WriteLine("Rows to be affected by command1: {0}", returnValue);

                // If you get here, this means that command1 succeeded. By nesting
                // the using block for connection2 inside that of connection1, you
                // conserve server and network resources as connection2 is opened
                // only when there is a chance that the transaction can commit.   
                using (SqlConnection connection2 = new SqlConnection(connectString2))
                {
                    // The transaction is escalated to a full distributed
                    // transaction when connection2 is opened.
                    connection2.Open();

                    // Execute the second command in the second database.
                    returnValue = 0;
                    SqlCommand command2 = new SqlCommand(commandText2, connection2);
                    returnValue = command2.ExecuteNonQuery();
                    writer.WriteLine("Rows to be affected by command2: {0}", returnValue);
                }
            }

            // The Complete method commits the transaction. If an exception has been thrown,
            // Complete is not  called and the transaction is rolled back.
            scope.Complete();

        }

    }
    catch (TransactionAbortedException ex)
    {
        writer.WriteLine("TransactionAbortedException Message: {0}", ex.Message);
    }
    catch (ApplicationException ex)
    {
        writer.WriteLine("ApplicationException Message: {0}", ex.Message);
    }

    // Display messages.
    Console.WriteLine(writer.ToString());

    return returnValue;
}

上記の例では、何をコミットしていますか?SQLクライアントライブラリはすべて正しく機能すると思いますか?これは、System.IO.StringWriterにすべての成功テキストまたはすべての失敗テキストが含まれることを意味しますか?または、TransactionScopeのスコープ間にロックはありますか?

4

2 に答える 2

3

まず第一に、TransactionScopeはtry/catchと同じではありません。TransactionScopeは、トランザクションの名前スコープによるものです。スコープ内のトランザクションは、スコープでCompleteを呼び出して明示的にコミットする必要があります。その他の場合(スコープで発生した例外を含む)は、スコープを破棄し、不完全なトランザクションを暗黙的にロールバックするブロックの使用を終了しますが、例外を処理しません。

基本的なシナリオでは、System.Transactionsからのトランザクションはdbクライアントトランザクションと同じように動作します。System.Transactionsは、次の追加機能を提供します。

  • APIにとらわれません。oracle、sqlサーバー、またはWebサービスに同じトランザクションスコープを使用できます。これは、永続性を認識しない(永続性の実装に関する情報を知らない)レイヤーでトランザクションを開始する場合に重要です。
  • 自動入隊。接続文字列で指定されている場合(デフォルトの動作)。新しいデータベース接続は、既存のトランザクションに自動的に参加します。
  • 分散トランザクションへの自動昇格。2番目の接続がトランザクションに参加すると、分散接続に自動的にプロモートされます(MSDTCが必要です)。プロモーションは、トランザクションWebサービスなどの他の調整されたリソースを参加させる場合にも機能します。
于 2010-08-21T10:21:10.873 に答える
1

トランザクションは、必要なロックを行います。また、(コメントで示唆されているように)Complete()によってコミットされていない場合、トランザクションがスコープの最後で破棄されると、暗黙のロールバックが発生します。したがって、例外が発生した場合、すべての操作が自動的にロールバックされ、データベースで変更は行われません。たとえば、2番目のクエリが失敗した場合、最初のクエリの変更も破棄されます。

ただし、StringWriterの場合は、障害が発生するまでのメッセージが含まれます(たとえば、

Rows to be affected by command1: {0}
ApplicationException Message: {0}

このコードの後に​​両方をログに表示できます。

このクラスでRDBMSを作成することに関しては、あなたの質問を理解できるかどうかはよくわかりません。リレーショナルdabase管理システムを実際に作成したいのであれば、おそらく間違った場所を見ていると思います。トランザクションを介してRDBMSにアクセスする場合は、ニーズによって異なります。ステートメントが順番に実行され、全か無​​かの方法で実行されることを保証できるトランザクションが必要な場合は、はい、トランザクションは開始するのに適した場所です。

于 2010-08-21T09:55:11.163 に答える