2011-11-18 3 views
6

私は次のような場合:TransactionScopeはParallel Extensionsで動作しませんか?

Using scope = New TransactionScope() 
     entries.Content.ReadAs(Of IList(Of WebMaint)).AsParallel.ForAll(Sub(entry) 
                      _repos.Update(entry) 
                     End Sub) 
     scope.Complete() 
    End Using 

のTransactionScopeが動作しません。 scope.completeにブレークポイントを置くと、トランザクションはアクティブではなく、更新は既に完了しています。

私はそれを変更した場合:

Using scope = New TransactionScope() 
      entries.Content.ReadAs(Of IList(Of WebMaint)).ToList().ForEach(Sub(entry) 
                       _repos.Update(entry) 
                      End Sub) 
      scope.Complete() 
End Using 

すべてが期待どおりに動作します。誰もがなぜ並列版が正しく動作しないのか知っていますか?

答えて

4

どのような技術であるかわかりませんが、通常はトランザクションはスレッドにバインドされており、スレッドには伝播しません。つまり、各スレッドで新しいトランザクションを開始する必要があります。しかしこれは、スレッドと同じくらい多くの独立したトランザクションを持つことを意味します。

トランザクションがシングルスレッドの基になるSQLデータベース接続に関連付けられているため、この制限は妥当です。

4

次のように、ワーカースレッドにトランザクションを伝播することができます

Using scope = New TransactionScope() 
    Dim rootTransaction As Transaction = Transaction.Current 

    entries.Content.ReadAs(Of IList(Of WebMaint)).AsParallel.ForAll(
     Sub(entry)  
      Dim dependentTransaction As DependentTransaction = rootTransaction.DependentClone(DependentCloneOption.RollbackIfNotComplete) 

      _repos.Update(entry) 

      dependentTransaction.Complete() 
     End Sub)   

    scope.Complete() 
End Using 

注:任意のVBの構文の問題を許してくださいは、「私の母国語は

ないTIS
関連する問題