c# – 将TransactionScope传递给Parallel.Invoke创建的任务
发布时间:2020-12-15 17:19:45 所属栏目:百科 来源:网络整理
导读:我希望并行运行的TxJobs能够从这个父事务创建一个范围.我该如何工作? using (var tx = TransactionScope()) { Parallel.Invoke(TxJob1,TxJob2) ; tx.Complete();} 我传入了一个DependentClone: using (var tx = new TransactionScope()) { var dtx1 = Tran
我希望并行运行的TxJobs能够从这个父事务创建一个范围.我该如何工作?
using (var tx = TransactionScope()) { Parallel.Invoke(TxJob1,TxJob2) ; tx.Complete(); } 我传入了一个DependentClone: using (var tx = new TransactionScope()) { var dtx1 = Transaction.Current.DependentClone( DependentCloneOption.RollbackIfNotComplete) ; var dtx2 = Transaction.Current.DependentClone( DependentCloneOption.RollbackIfNotComplete) ; Parallel.Invoke(() => TxJob1(dtx1),() => TxJob2(dtx2)) ; tx.Complete(); } 在TxJob1和TxJob2方法中,如果我只是在DependentClones上调用Complete,它就可以工作.但是,如果我从克隆创建一个范围,我会得到一个TransactionAbortedException: void TxJob1(Transaction dt) { using (var tx = new TransactionScope(dt)) { Console.WriteLine(dtx.TransactionInformation.LocalIdentifier); tx.Complete(); } } 在main方法中调用Complete,而不是在TxJobs中引发异常.为什么这会失败? [编辑]如果我在TxJobs中的DependentTransaction上显式调用Complete,那么它可以工作.如果我没有在TxJobs中的新TransactionScope上调用Complete(触发回滚),则父事务失败.看起来我必须在两个Transaction对象上调用Complete. 解决方法
看起来我必须在依赖克隆和TransactionScope上调用Complete. MS在
sample code中做同样的事情.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |