Using distributed transactions in .Net 1.x without deriving from ServicedComponent

c++多线程(1)------创建和管理线程td::thread std::thread 是 C++11 标准库中用于创建和管理线程的核心类,定义在 <thread> 头文件中。它使得多线程编程变得简单、类型安全且跨平台。 阅读详情

引用 : http://blogs.msdn.com/florinlazar/archive/2004/07/24/194199.aspx

The most used feature of System.EnterpriseServices or COM+ is the distributed transaction support. And the automatic transaction programming model in ES using attributes ([Transaction] and [AutoComplete]) is great and nice but (it is always a but!)... you need to inherit from ServicedComponent and the Transaction attribute is only available at class level, and you need to register your component in the COM+ repository and the list can continue.

 

If doing this seems overkill to you, because all you need is a distributed transaction to protect your code/actions and you don't care of any of the others ES features (which are great ones nevertheless) then there is a solution for you: System.EnterpriseServices.ServiceDomain. Here is some sample code:

 

using System;

using System.EnterpriseServices;

 

namespace SDSample

{

   class Class1

   {

      [MTAThread]      

      static void Main (string[] args)

      {

         ServiceConfig config = new ServiceConfig();

         config.Transaction = TransactionOption.Required;

         ServiceDomain.Enter(config);

         try

         {

            MyTxCode();

         }

         catch(Exception e)

         {

            // we got an exception

            Console.WriteLine(e.Message);

            // so, we should abort the transaction

            ContextUtil.SetAbort();

         }

         finally

         {

            ServiceDomain.Leave();

         }

      }

 

      // The code that I want to be transactional

      static void MyTxCode()

      {

         Console.WriteLine(ContextUtil.TransactionId);

                 

         // Open connection to database 1

         // Execute update in database 1

 

         // Open connection to database 2

         // Execute update in database 2

      }

   }

}

 

Of course, you can go further and create a helper class, let’s call it ESTransactionScope (similar to System.Transactions.TransactionScope that will arrive in Whidbey) that will be very easy to use:

 

using System;

using System.EnterpriseServices;

 

namespace SDSample2

{

   class Class1

   {

      [MTAThread]      

      static void Main (string[] args)

      {

         using( ESTransactionScope ts = new ESTransactionScope())

         {

           MyTxCode();

 

           // Everything went well, no exception thrown

           // so let’s vote for Commit

           ts.Complete();

         }

      }

 

      static void MyTxCode()

      {

         Console.WriteLine(ContextUtil.TransactionId);

                 

         // Open connection to database 1

         // Execute update in database 1

 

         // Open connection to database 2

         // Execute update in database 2             

      }

   }

 

   // Used to create transactional code blocks

   class ESTransactionScope : IDisposable

   {

      // Dispose must be called to exit the transactional block

      public void Dispose()

      {                

         if(!this.Consistent)

         {

            ContextUtil.SetAbort();

         }

         ServiceDomain.Leave();

      }

 

      // by calling this method, you mark the scope as being consistent

      // and ready to for commit

      // if the method is never called, upon dispose, the scope will abort the transaction 

       public void Complete()

      {

         this.Consistent = true;

      }   

 

      public ESTransactionScope()

      {                

         EnterTxContext(TransactionOption.Required);

      }

 

      public ESTransactionScope(TransactionOption txOption)

      {

         EnterTxContext(txOption);

      }

 

      private void EnterTxContext(TransactionOption txOption)

      {

         ServiceConfig config = new ServiceConfig();

         config.Transaction = txOption;

         ServiceDomain.Enter(config);          

      }

 

      // By default, the scope is inconsistent;

      // To Commit the transaction on exit, the Consistent flag

      // must be set to true before Dispose is called

      private bool Consistent = false;

   }

}

 

System.EnterpriseServices.ServiceDomain is available only on XP SP2 (or higher) and Windows Server 2003 and only in .Net 1.1.

 

If you need your app to work with .Net 1.0 or on Windows 2000 or XP pre-SP2, you can use the trick that Don Box posted at http://www.gotdotnet.com/team/dbox/default.aspx?key=2004-07-12T08:40:44Z  It uses exactly one transactional ServicedComponent based class and a DoCallback method to which you pass the delegate to your MyTxCode function that needs to execute in a transaction.

FFDNet-pytorch版本代码训练教程 该文章为FFDNet-pytorch版本代码训练教程,抓紧订阅学习!尽早入门发论文哦! 阅读详情

相关推荐

Nettrace 源码分析

文章介绍:本文将围绕腾讯开源项目nettrace工具中数据采集,数据处理,数据分析三个模块深入源码分析其实现细节

abobob的博客 994

[引用]dotNET下面调用Access中存储过程的方法

http://blog.csdn.net/yohomonkey/archive/2004/09/30/121555.aspx在Sql Server中使用存储过程是大家都比较熟悉的了,前不久自己作了一个小东东,数据库使用的是MS Access数据库,也想着试试用存储过程是否可行。毕竟,存储过程与代码的关系不是非常大,数据库和存储过程的修改有些是不用更改编码的。下面贴出我的代码,大家会发现,其实跟调用

Sharper的技术人生 6613

STM32F407驱动OV2640摄像头:从SCCB协议到I2C模拟的保姆级避坑指南

本文详细介绍了STM32F407驱动OV2640摄像头的完整流程,重点解析SCCB协议与I2C的关键差异,并提供GPIO模拟实现方案。通过实战案例和调试技巧,帮助开发者避开常见陷阱,实现稳定的图像采集功能。

weixin_28700411的博客 353

错误“操作必须使用一个可更新的查询”原因及解决办法

错误信息:       Microsoft OLE DB Provider for ODBC Drivers error 80004005        [Microsoft][ODBC Microsoft Access 97 Driver] Operation must use an       updateable query. 原因:       有几个主要的错误原因:       这个

Sharper的技术人生 5202

基于STM32智能震动报警器震动检测声光报警系统设计

*单片机设计介绍,基于STM32智能震动报警器震动检测声光报警系统设计。

2201_75940997的博客 2159

STM32F103(二十三)通用同步异步收发器(USART)

学习板:STM32F103ZET6 往期博客: STM32F103五分钟入门系列(一)跑马灯(库函数+寄存器)+加编程模板+GPIO总结 STM32F103五分钟入门系列(二)GPIO的七大寄存器+GPIOx_LCKR作用和配置 STM32F103五分钟入门系列(三)GPIO的常用库函数使用方法总结+一个网络上的误区 STM32F103五分钟入门系列(四)蜂鸣器实验(库函数+寄存器) STM32F103五分钟入门系列(五)按键实验(库函数+寄存器) STM32F103五分钟入门系列(六)时钟框图+相关寄存器

Curnane0_0的博客 1025

在Window2003上执行System.Diagnostics.Process.GetProcessesByName等方法失败的原因

在Windows2003上执行 System.Diagnostics.Process.GetProcessesByName等方法会报出Exception:Couldnt get process information from remote machine.这个错误的原因是: 获取Process与Performance Counter相关, 当前的执行用户不是Administrator 组成员M

Sharper的技术人生 4086

机器学习项目实战案例数据集

本专栏整理了《机器学习项目实战案例》,内包含了各种不同的机器学习项目,包含项目原理以及源码,每一个项目实例都附带有完整的代码+数据集。

CSDN 精品推荐 3647

[转]使用.net 程序生成 Excel 和Access 文件

 转至  http://blog.joycode.com/ghj/archive/2005/01/12/42861.aspx 一些系统可能需求把数据导出到Access或者Excel文件格式,以方便的传递数据、打印等。Excel 文件或者 Access这两种需要导出的文件可能并不是事先就存在的,这就需要我们自己编程生成他们,下面整理一下生成这两个文件的一些方法,只罗列最常用的。并不全。一、首

Sharper的技术人生 2848

图注意力网络(GAT)原理与代码实战案例讲解

1. 背景介绍 图注意力网络(Graph Attention Network,简称GAT)是一种基于图神经网络(Graph Neural Networks,简称GNN)的深度学习方法。它能够处理具有复杂关系的节点数据,并在多个领域取得了显著的效果,如社交网络分析、推荐系统等。 2. 核心概念与联系

AI架构师小马 2728

【CANdelaStudio编辑CDD】-2.2-给$27服务SecurityAccess创建一个SecurityLevel安全级别并修改SecurityAccess安全访问状态机(UDS诊断)

前一篇: 【UDS诊断描述文件CDD】-2.1-ISO_14229中$27服务理论部分https://blog.csdn.net/qfmzhu/article/details/120392660 案例背景: 在CDD中如何给$27服务创建一个新的SecurityLevel安全级别; 在CDD中如何给$27服务修改SecurityAccess安全访问状态机。 1 查看$27服务中已有的SecurityLevel安全级别 2 查看$27服务中已有的SecurityAccess安全...

「汽车电子助手」的博客 4661

关于 在 HttpModule 中使用 System.Thread.Timer

 在HttpModule中使用 System.Thread.Timer 可以在一定程度上实现 定时任务/后台任务 //public class ScheduleMailModule : IHttpModule {  private static Timer _timer;  public ScheduleMailModule()  {  }   #region IHttpModule 成员  

Sharper的技术人生 2449

大数据利器Hadoop:从基础到实战,一篇文章掌握大数据处理精髓!

在当今大数据时代,数据量的爆炸式增长对企业和技术提出了前所未有的挑战。如何高效地存储、处理和分析这些庞大的数据集,成为了亟待解决的问题。Hadoop作为一种分布式计算框架,应运而生,为大数据处理提供了有效的解决方案。Hadoop是一个由Apache软件基金会维护的开源项目,它基于Google的分布式文件系统(Google File System,GFS)和MapReduce计算模型设计。Hadoop的主要目标是处理大规模数据集,它可以在普通硬件集群上运行,从而降低了大数据处理的成本。

邓邓子的博客 8321

DataGrid 动态列

英文原文: http://scottwater.com/articles/DynamicItemTemplates中文翻译: http://forum.newtopworks.com/ShowPost.aspx?PostID=2113动态ItemTemplate的实现(译)(转) 原文: Implementing Dy

Sharper的技术人生 2306

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

关于ASP.net中的PathInfo

原文: http://west-wind.com/weblog/posts/269.aspx Request PropertyFunction and ExampleApplicationPathReturns the a Web server relative path to your application root/WestwindWe

Sharper的技术人生 1760

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

点击 "文件下载"弹出窗口 中的 "打开" 失败的可能原因

在aspx中 加入下面 OuputCache 指令将导致 点击 "文件下载"弹出窗口 中的 "打开" 失败!

Sharper的技术人生 1667

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

使用你自己的浏览器打开新窗口

选择自 http://dev.csdn.net/user/2066  的 Blog If you trap the NewWindow2 event for the WebBrowser control, you can stop all new windows opening, like this: Code: Private Sub AxWebBrowser1_NewWindow2(B

Sharper的技术人生 1453

在dotnet中如何使用资源(转自MS中国社区)

如何使用资源文件 摘要 .NET 中有一套非常完善的地方化系统被定义在 System.Resources 名字空间中。不过大多数人都被 MissingManifestResourceException 这个错误困惑着。本文就是要让大家了解什么是资源文件,它有什么用处以及如何正确的调用从而避免一些"奇怪"的错误,包括 MissingManifestResourceException 这个常见错误

Sharper的技术人生 1441

[引用]C#钩子本线程内消息拦截

http://blog.csdn.net/yiruoyun/archive/2004/10/17/140219.aspx钩子其实就是调用一下API而已:1、安装钩子:   SetWindowsHookEx    函数原形:HHOOK SetWindowsHookEx(                       int       idHook,    // 钩子类型,          

Sharper的技术人生 1394

How to deploy a .Net assmebly for COM use through CAB on Web Page (转)

http://blog.joycode.com/felix/articles/32905.aspx

Sharper的技术人生 1384

ASP.NET 应用程序的进程以及用户模拟

INFO:ASP.NET 中的进程和请求标识: http://support.microsoft.com/kb/q317012/在 ASP.NET 应用程序中实现模拟: http://support.microsoft.com/kb/q306158/ 可以进行 特定代码段进行模拟, 以提升线程的权限

Sharper的技术人生 1326

List of the Most Common Data Type Mappings

http://support.microsoft.com/default.aspx?scid=kb;en-us;320435 The following table lists the most common data types that are used in Microsoft Access and how these data types relate to the Microsoft .

Sharper的技术人生 1271

把 Mission Planner 搬上鸿蒙 PC:把 .NET 内核烧成 so,移植鸿蒙应用

将 Mission Planner 移植至鸿蒙 PC,采用 .NET NativeAOT 编译内核为无 JIT 的 libmagcs.so,通过 C ABI 与 NAPI 桥接,实现纯原生运行。以 ArkUI 重写外壳,保留核心协议与状态逻辑,支持 MAVLink 心跳、参数/任务传输及多链路通信。项目完成度高,仅地图瓦片与固件烧写待完善,验证了“内核原样迁移 + 外壳重构”的 T1 级适配路径,为开源鸿蒙生态注入关键飞行控制能力。

轻口味的专栏 5391

.NET 8 + 信创双栈:智慧文旅平台的多渠道票务与数据中台架构复盘

中心库存、幂等发码、多库方言适配、断网补传:一套文旅票务中台的架构取舍与踩坑复盘。

侯炯的专栏 206

VS Code 配置 C# 开发环境(.NET 10 LTS)

本文详述如何用轻量级的 VS Code 与 .NET 10 LTS 构建高效 C# 开发环境。涵盖 SDK 安装(含离线加速)、扩展配置、项目创建、F5 断点调试及 NuGet 管理,全程实测可用。无需安装 Visual Studio,跨平台支持,适合现代 C# 开发,特别推荐新项目使用 .NET 10 LTS。

Joey 的工作空间~ 521

Aspose.Slides for .NET v26

  Aspose.Slides for .NET allow the users to read, compose, modify, clone, shield, and even combine the PowerPoint under OpenOffice demonstration in the platform of .NET C# without having any external applications. It is a demo that is processing the API fo

q2315702359的博客 252
上一篇: ASP.NET 应用程序的进程以及用户模拟
下一篇: [转]使用.net 程序生成 Excel 和Access 文件
xdev
博客等级 码龄25年 1粉丝 33原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址