TransactionScope 无法与并行扩展一起使用?
如果我执行以下操作:
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
一切都按预期进行。有人知道为什么并行版本不能正常工作吗?
If i do the following:
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 doesn't work. If i put a breakpoint on the scope.complete no transaction is active and the updates are already complete.
If i change it to:
Using scope = New TransactionScope()
entries.Content.ReadAs(Of IList(Of WebMaint)).ToList().ForEach(Sub(entry)
_repos.Update(entry)
End Sub)
scope.Complete()
End Using
Everything works as expected. Anyone know why the parallel version doesn't work correctly?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不知道它是什么技术,但通常事务是线程绑定的并且不会传播到子线程。话虽如此,您必须在每个线程中启动一个新事务。但这意味着您将拥有与线程一样多的独立事务。
此限制是合理的,因为事务附加到单线程的底层 SQL 数据库连接。
I have no idea what technology is it, but typically transactions are thread bound and do not propagate to children threads. That being said you will have to start a new transaction in each thread. But this means you will have as many independent transactions as threads.
This limitation is reasonable since the transaction is attached to the underlying SQL database connection which is single threaded.
您可以将事务传播到工作线程,如下所示:
注意:请原谅任何 VB 语法问题,这不是我的母语
You can propagate the transaction to the worker threads as follows:
NOTE: please forgive any VB syntax issues, 'tis not my native tongue