首页 / 软件开发 / .NET编程技术 / 谈谈分布式事务之三 System.Transactions事务详解[上篇]
谈谈分布式事务之三 System.Transactions事务详解[上篇]2011-08-20 博客园 Artech在.NET 1.x中,我们基本是通过ADO.NET实现对不同数据库访问的事务。.NET 2.0为了带来了全新的 事务编程模式,由于所有事务组件或者类型均定义在System.Transactions程序集中的 System.Transactions命名空间下,我们直接称基于此的事务为System.Transactions事务。 System.Transactions事务编程模型使我们可以显式(通过System.Transactions.Transaction)或者隐 式(基于System.Transactions.TransactionScope)的方式进行事务编程。我们先来看看,这种全新的 事务如何表示。一、System.Transactions.Transaction在System.Transactions事务体系下,事务本身通过类型System.Transactions.Transaction类型表示 ,下面是Transaction的定义:1: [Serializable]
2: public class Transaction : IDisposable, ISerializable
3: {
4: public event TransactionCompletedEventHandler TransactionCompleted;
5:
6: public Transaction Clone();
7: public DependentTransaction DependentClone(DependentCloneOption cloneOption);
8:
9: public Enlistment EnlistDurable(Guid resourceManagerIdentifier, IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions);
10: public Enlistment EnlistDurable (Guid resourceManagerIdentifier, ISinglePhaseNotification singlePhaseNotification, EnlistmentOptions enlistmentOptions);
11: public bool EnlistPromotableSinglePhase(IPromotableSinglePhaseNotification promotableSinglePhaseNotification);
12: public Enlistment EnlistVolatile (IEnlistmentNotification enlistmentNotification, EnlistmentOptions enlistmentOptions);
13: public Enlistment EnlistVolatile (ISinglePhaseNotification singlePhaseNotification, EnlistmentOptions enlistmentOptions);
14:
15: public void Rollback();
16: public void Rollback(Exception e);
17:
18: void ISerializable.GetObjectData (SerializationInfo serializationInfo, StreamingContext context);
19:
20: public static Transaction Current { get; set; }
21:
22: public IsolationLevel IsolationLevel { get; }
23: public TransactionInformation TransactionInformation { get; }
24: }
1、Transaction是可序列化的从上面的定义我们可以看到,Transaction类型(在没有特殊说明的情况下,以下的Transaction类型 指的就是System.Transactions.Transaction)上面应用的SerializableAttribute特性,并且实现了 ISerializable接口,意味着一个Transaction对象是可以被序列化的。Transaction的这一特性在WCF整 个分布式事务的实现意义重大,原因很简单:要让事务能够控制整个服务操作,必须实现事务的传播, 而传播的前提就是事务可被序列化。