TransactionScope 和多线程

TransactionScope and multi-threading(TransactionScope 和多线程)
本文介绍了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 和多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to register System.DirectoryServices for use in SQL CLR User Functions?(如何注册 System.DirectoryServices 以在 SQL CLR 用户函数中使用?)
Query From LDAP for User Groups(从 LDAP 查询用户组)
How can I get DOMAINUSER from an AD DirectoryEntry?(如何从 AD DirectoryEntry 获取 DOMAINUSER?)
Get a list of members of a WinNT group(获取 WinNT 组的成员列表)
Get UPN or email for logged in user in a .NET web application(在 .NET Web 应用程序中获取登录用户的 UPN 或电子邮件)
Active Directory Group Membership Checking in .Net 4.5(.Net 4.5 中的 Active Directory 组成员身份检查)