2016-10-12 3 views
0

に私はこのようなTransactionScopeを追加するこの複数TransactionScope.Complete()を使用して

try 
{ 
    MyModel model = repo.GetData(); 
    if(model == null) 
    { 
     return model; 
    } 
    else 
    { 
     MyResponse response = checkData(); 
     if(response) 
     { 
      return model; 
     } 

     UpdateData(); 
    } 
} 
catch(Exception e) 
{ 
    .... 
} 
return model; 

のようなコードを持っています。

try 
{ 
    using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions { IsolationLevel = IsolationLevel.ReadCommitted })) 
    { 
     MyModel model = repo.GetData(); 
     if(model == null) 
     { 
      return model; 
     } 
     else 
     { 
      MyResponse response = checkData(); 
      if(response) 
      { 
       return model; 
      } 

      UpdateData(); 
     } 
     ts.Complete(); 
    } 
} 
catch(Exception e) 
{ 
    .... 
} 
return model 

そして私は尋ねたい、それはコードがts.Complete()に到達する前に、私は複数のreturn文を持っている場合は大丈夫でしょうか? TransactionScopeがtryブロック内にあるので、finallyブロックでtsをヌルに設定できません。

答えて

1

あなたの質問に答えるには:trans.Complete()の前に戻るのは大丈夫ですか? - はい、そうです。取引はコミットされず、変更は適用されません。それがどのように機能するかです。

usingステートメントは、例外が発生している(または戻る)場合でも、すべてのシナリオで.Dispose()を実行します。

トランザクションを破棄するときは、trans.Complete()が呼び出された場合はコミットされ、呼び出されない場合は変更が適用されません。 Transaction.Dispose()について What is the C# Using block and why should I use it?

:ステートメントを使用について

https://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.dispose(v=vs.110).aspx

関連する問題