TransactionScope 和多线程 [英] TransactionScope and multi-threading

查看:31
本文介绍了TransactionScope 和多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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