.NET开发

本类阅读TOP10

·NHibernate快速指南(翻译)
·vs.net 2005中文版下载地址收藏
·【小技巧】一个判断session是否过期的小技巧
·VB/ASP 调用 SQL Server 的存储过程
·?dos下编译.net程序找不到csc.exe文件
·通过Web Services上传和下载文件
·学习笔记(补)《.NET框架程序设计(修订版)》--目录
·VB.NET实现DirectDraw9 (2) 动画
·VB.NET实现DirectDraw9 (1) 托管的DDraw
·建站框架规范书之——文件命名

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Using distributed transactions in .Net 1.x without deriving from ServicedComponent

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

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




相关文章

相关软件