的TransactionScope不工作并行扩展?工作、TransactionScope

2023-09-05 22:59:11 作者:嫣然一笑终成梦

如果我做到以下几点:

 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.