的TransactionScope不工作并行扩展? [英] TransactionScope not working with Parallel Extensions?

查看:120
本文介绍了的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没有事务是有效和更新都已经完成了。

TransactionScope doesn't work. If i put a breakpoint on the scope.complete no transaction is active and the updates are already 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

一切按预期工作。任何人都知道为什么水货版本无法正常工作?

Everything works as expected. Anyone know why the parallel version doesn't work correctly?

推荐答案

我不知道什么样的技术呢,但通常交易线程约束,并不会传播到孩子的线程。话虽这么说,你将不得不开始一个新的事务中的每个线程。但是,这意味着你将有许多独立的交易为主​​题。

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.

此限制是合理的,因为该事务是附着到基础SQL数据库连接,是单线程

This limitation is reasonable since the transaction is attached to the underlying SQL database connection which is single threaded.

这篇关于的TransactionScope不工作并行扩展?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆