惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - coollzh

关于.NET VS JavaEE平台争论的沉思录 不要使用Microsoft Project的理由 .NET下的开发者们正在继承计算机早期时代伟大的黑客精神 上海著名网络公司招聘高级软件工程师 即将过的2004 URLRewriting的问题 谁知道c++中的char ** 在C#中一般用什么类型 Indigo Untyped Channel VC#2005 最新技术预览版下周发布 上海某著名互联网公司招聘asp.net/.net开发工程师 Coming soon: “Enterprise Library” .net framework1.1到2.0的重大变化 Windows 2003 中DTC的怪事情 动态Validator的奇怪问题 一件及其汗的事! sql server2000 在windows2003的默认安装客户端无法连接 msn”给你一个惊喜“的病毒 DataReader的问题 35岁前应该做好的十件事
Using distributed transactions in .Net 1.x without deriving from ServicedComponent
coollzh · 2005-03-09 · via 博客园 - coollzh

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

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.