0

SQL Server 2008 R2 を使用しており、トランザクションを使用しようとしています。

最初に .net と SQL Server のトランザクションに関する質問です。こんなのあったら

try {
    var transactionOption = new TransactionOptions();
    transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
    transactionOption.Timeout = TransactionManager.MaximumTimeout;

    using (var scope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOption)) {
        //create question this creates a new question in the database
        Helpers.CreateQuestionBankItem(ref mappedOldNewQuestionItemGuid, missingQuestionBankItems);
        //question created

        //query database for the code of the newly inserted question, will the database give me the code since Complete has not been called as yet?    



        scope.Complete();
    }
}
catch (Exception ex) {
    throw;
}

//query database for the code of the newly inserted question,  will the database give me the code since Complete has been called as now?    

どの時点でデータベースを呼び出して、新しく挿入された質問のコードを要求する必要がありますか。2 番目の質問です。質問する前に、このリンクNested Transactionを見つけました。上記のリンクに照らして、私はこのようなものがあるかどうかまだ尋ねたいです

try {
    var transactionOption = new TransactionOptions();
    transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
    transactionOption.Timeout = TransactionManager.MaximumTimeout;

    using (var outerscope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOption)) {
        try {
            var transactionOption = new TransactionOptions();
            transactionOption.IsolationLevel = IsolationLevel.ReadCommitted;
            transactionOption.Timeout = TransactionManager.MaximumTimeout;

            using (var innerscope = new TransactionScope(TransactionScopeOption.RequiresNew, transactionOption)) {
                //create question this creates a new question in the database
                Helpers.CreateQuestionBankItem(ref mappedOldNewQuestionItemGuid, missingQuestionBankItems);
                //question created

                //query database for the code of the newly inserted question, will the database give me the code since Complete has not been called as yet?    

                innerscope.Complete();
            }
        }
        catch (Exception ex) {
        }

        //query database for the code of the newly inserted question,  will the database give me the code since Complete has been called as now?    

        outerscope.Complete();
    }
}
catch (Exception ex) {
    throw;
}

内部スコープが完了すると、SQL Server にクエリを実行すると、新しく作成された質問のコードが表示されます。

内側のスコープが例外をスローし、それをむさぼり食うとどうなりますか?外側のスコープも破棄されますか?

innerscope.Complete() を呼び出すと、その内部スコープが完成しますか?

4

1 に答える 1

3

トランザクション コンテキストで障害から回復したい場合は、トランザクション セーブポイントを使用する必要があります。残念ながら、マネージSystem.Transactionはセーブポイントをサポートしていません。それだけでなく、トランザクション スコープを使用する場合、セーブポイントを直接使用することさえできません。これは、トランザクション スコープが分散トランザクションにエスカレートし、セーブポイントが分散コンテキストでは機能しないためです。

代わりに、セーブポイントSqlTransactionをサポートするプラットフォーム固有のものを使用できます。トランザクション対応の例外処理の例については、例外処理とネストされたトランザクションSave()を参照してください。

于 2013-04-25T13:08:32.307 に答える