问题描述
我想知道在处理多线程时如何以正确的方式使用 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 和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)