0

このように TransactionScope が別の内部にネストされているコードの例を見てきました

using(TransactionScope scope1 = new TransactionScope())
{
     try
     {
          //Start of non-transactional section 
          using(TransactionScope scope2 = new
             TransactionScope(TransactionScopeOption.Suppress))
          {
               //Do non-transactional work here
          }
          //Restores ambient transaction here
   }
     catch
     {}
   //Rest of scope1
}

私はここでの使用を理解できますが、私が理解しているSupress限りRequired、外側のトランザクションとマージするだけなので、何かが失敗した場合、すべてが失敗するので、ポイントは何ですか? ここで何か不足していますか?

編集: 明確にするために、MSDN のドキュメントで説明されている Suppress オプション (と思います:-)) を理解していることを強調したいと思います。私の質問は、デフォルトの必須オプションです。たぶん私は完全には理解していませんが、トランザクション B がトランザクション A 内にネストされている場合、A が失敗するかB が失敗すると、ABの両方がロールバックされます。これは、最初に B をトランザクションに入れなかった場合と同じです.

したがって、言い換えれば、「Transaction をデフォルトの Required オプションでネストすることと、すべてを実行しないことの違いは何ですか?」'

4

1 に答える 1

0

これについて考えます:

using(TransactionScope scope1 = new TransactionScope())
{
     //i insert some records in tabe A
     try
     {              
          using(TransactionScope scope2 = new
             TransactionScope(TransactionScopeOption.Suppress))
          {                  
               // I insert some records in table B
               scope2.Complete() 
          }
          //I insert some records in table C
   }
     catch
     {}
   //I Update some records in table D
   //I Delete some records of table E
   // Here we have an exception being thrown                       
 scope1.Complete() 
}

コードを実行した後、
テーブルA、C、D、Eへ
のすべての変更がロールバックされ、テーブルBへのすべての変更がコミットされます

TrnsactionScopダウンサイド

于 2013-02-11T19:07:39.230 に答える