我想知道在处理多线程时如何以正确的方式使用 TransactionScope 类?
I was wondering how you would use the TransactionScope class in the correct way when you are dealing with multithreading?
我们在主线程中创建了一个新的作用域,然后我们产生了几个工作线程,我们希望这些线程参与到主作用域中,例如,如果作用域从未完成,则在每个工作线程上调用回滚.
We create a new scope in our main thread and then we spawn off a couple of worker threads and we want these to participate in the main scope, so that for example the rollback is called on each worker if the scope is never completed.
我在内部使用 ThreadStaticAttribute 阅读了有关 TransactionScope 的内容,这使得上述操作变得不可能/非常困难 - 有人可以验证任何一种方式吗?如果我们以同步方式运行代码,则回滚工作,即内部事务能够参与主事务,但如果我们切换到线程执行则不行.
I read something about TransactionScope using the ThreadStaticAttribute internally which made the above impossible / very difficult - could someone verify either way? If we run our code in a synchronized fashion then the rollbacks work, i.e the inner transactions are able to participate in the main transaction, but not if we switch over to a threaded execution.
参见 MSDN:
您还应该使用 TransactionScope 和DependentTransaction 类需要使用的应用程序同一笔交易跨多个函数调用或多线程电话.
You should also use the TransactionScope and DependentTransaction class for applications that require the use of the same transaction across multiple function calls or multiple thread calls.
所以也许看看 DependentTransaction
- 特别是,有一个工作线程示例,这里.
So maybe look into DependentTransaction
- in particular, there is a worker thread example, here.
这篇关于TransactionScope 和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!