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

引用 : 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值