转自:享受代码,享受人生

http://www.cnblogs.com/idior/archive/2005/08/15/214300.html

 

在谈具体实现前 先介绍一下三种事务:
1. 单对象单资源
【转】Transaction in ADO.net 2.0
2. 多对象单资源
【转】Transaction in ADO.net 2.0

3. 多对象多资源(分布式事务, 使用两段提交协议)
【转】Transaction in ADO.net 2.0

在ADO.Net1.0下有两种使用Transaction的方法. 一种是在需要事务的对象中显式的调用事务处理, 还有一种是使用Enterprise Service的声明式的方法.

第一种方法的示例代码如下:

【转】Transaction in ADO.net 2.0      public void TransactionTest()

这种方法使用起来相当麻烦, 而且只能针对一个对象访问一个资源的事务. 如果事务涉及多个对象,那么由谁来进行事务处理?  如果事务使用了多个资源, 那样又涉及到分布式事务和两段提交协议,此时依靠第一种方法完全由用户自己控制实在过于复杂,因此在提供了第一种基本方法后, ADO.Net 1.0又利用Com+实现了声明式的事务处理,示例代码如下:

【转】Transaction in ADO.net 2.0using System.EnterpriseServices;
【转】Transaction in ADO.net 2.0[Transaction]
【转】Transaction in ADO.net 2.0
public class MyComponent : ServicedComponent
}

这种声明式的方法看上去似乎很好,但是也隐含了许多问题.
1. 使用事务的对象需要继承ServicedComponent
2. 即使不涉及多资源的分布式事务而仅仅是涉及到了多个对象的简单事务(开头介绍的第二种事务),我也要使用此方法,影响了效率. 这样的弊端和J2ee中的都在本地的Entity Bean之间进行通讯很像,杀鸡也不得不用牛刀.
3. 不可避免的使用了Com+.
4. 使用Enterprise Services的事务总是线程安全的, 也就是说你无法让多个线程参与到同一个事务中.

ADO.Net2.0 提供的新的事务模型综合了前两者的优点,
1 在简单(不涉及分布式)事务中也可以使用声明式的事务处理方法, 而不必使用Com+容器, ADO.net 2.0中提供了一个轻量级的事务容器.
2 用户根本不需要考虑是简单事务还是分布式事务. 新模型会自动根据事务中涉及的对象资源判断使用何种事务管理器. 简而言之, 对于任何的事务用户只要使用同一种方法进行处理. 示例代码:

【转】Transaction in ADO.net 2.0using(TransactionScope scope = new TransactionScope())
}

另外对嵌套事务和事务的隔离级别也提供了支持, 在此就不作详细介绍. Fantasy Soft对此做了介绍.

【转】Transaction in ADO.net 2.0using(TransactionScope scope1 = new TransactionScope())
【转】Transaction in ADO.net 2.0
//Default is Required
}

【转】Transaction in ADO.net 2.0

【转】Transaction in ADO.net 2.0

【转】Transaction in ADO.net 2.0TransactionOptions options = new TransactionOptions();
【转】Transaction in ADO.net 2.0options.IsolationLevel 
= IsolationLevel.ReadCommitted;
【转】Transaction in ADO.net 2.0options.Timeout 
= TransactionManager.DefaultTimeout;
【转】Transaction in ADO.net 2.0
using(TransactionScope scope = new TransactionScope(TransactionScopeOption.Required, options))

 

【转】Transaction in ADO.net 2.0public enum IsolationLevel
}

本文仅仅是ADO.net 2.0 Transaction的简单介绍, 详细资料查看M$的相关文档.

参考资料:  Introducing System.Transactions  by Juval Lowy

相关文章:

  • 2021-11-04
  • 2021-12-14
  • 2021-12-29
  • 2022-12-23
  • 2021-10-11
  • 2021-08-13
  • 2021-07-02
  • 2021-12-07
猜你喜欢
  • 2021-09-17
  • 2021-11-11
  • 2021-09-27
  • 2021-11-14
  • 2021-11-22
相关资源
相似解决方案