TransactionScope 和多线程
- 作者: 曲短情长丶
- 来源: 51数据库
- 2022-10-20
问题描述
我想知道在处理多线程时如何以正确的方式使用 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.
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
