您现在的位置:中国下载站学院中心网络编程ASP.net教程Asp.Net开发技巧 → 文章列表

在.NET 2.0 中使用自定义事务操作

作者:佚名  来源:不详  发布时间:2006-12-29 0:18:37   

减小字体 增大字体

 
 
.net 2.0 framework 中新增了 System.Transactions 命名空间,其中提供的一系列接口和类使得在.net 2.0 中使用事务比起从前要方便了许多。有关在 .net 2.0 下操作数据库事务的文章已经有了很多,这里只提一下如何设计自定义事务操作。

  一、事务使用基础

  先看一段使用事务的代码:

1using (TransactionScope ts= new TransactionScope())
2{
3 //自定义操作
4 ts.Complete();
5}

  这里使用 using 语句定义了一段隐性事务。如果我们在该语句块中加入一段对 SQL Server 操作的代码,那么它们将会自动加入这个事务。可以看出,这种事务的使用方式是极其方便的。

  那么,有没有可能在该语句块中加入我们自己定义的事务操作,并且该操作能够随着整个事务块的成功而提交,随其失败而回滚呢?答案当然是可以的,否则我就不会写这篇随笔了。

  二、实现自定义事务操作

  根据事务的特性,我们可以推想:这个操作必须有实现提交和回滚之类动作的方法。没错,这就是 System.Transactions 命名空间中的 IEnlistmentNotification 接口。我们先写一个最简单的实现:

1class SampleEnlistment1 : IEnlistmentNotification
2{
3 void IEnlistmentNotification.Commit(Enlistment enlistment)
4 {
5 Console.WriteLine("提交!");
6 enlistment.Done();
7 }
8
9 void IEnlistmentNotification.InDoubt(Enlistment enlistment)
10 {
11 throw new Exception("The method or operation is not implemented.");
12 }
13
14 void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
15 {
16 Console.WriteLine("准备!");
17 preparingEnlistment.Prepared();
18 }
19
20 void IEnlistmentNotification.Rollback(Enlistment enlistment)
21 {
22 Console.WriteLine("回滚!");
23 enlistment.Done();
24 }
25}
26
27

  好,定义完之后,还需要向事务管理器进行注册,把它加入到当前事务中去:

1using (TransactionScope ts= new TransactionScope())
2{
3 SampleEnlistment1 myEnlistment1 = new SampleEnlistment1();
4 Transaction.Current.EnlistVolatile(myEnlistment1, EnlistmentOptions.None);
5 ts.Complete();
6}

  执行这一段代码,我们可以得到以下的输出:

  准备!
  提交!

  先解释一下,当调用 ts.Complete() 方法的时候,表示事务已成功执行。随后,事务管理器就会寻找当前所有已注册的条目,也就是 IEnlistmentNotification 的每一个实现,依次调用它们的 Prepare 方法,即通知每个条目做好提交准备,当所有条目都调用了 Prepared() 表示自己已经准备妥当之后,再依次调用它们的 Commit 方法进行提交。如果其中有一个没有调用 Prepared 而是调用了 ForceRollback 的话,整个事务都将回滚,此时事务管理器再调用每个条目的 Rollback 方法。

  而如果我们将前面的 ts.Complete() 行注释掉,显然执行结果就将变为:

  回滚!

  三、一个实现赋值的自定义操作

  考虑一下,我们要实现一个事务赋值操作。该如何做法?以下是一个例子:

1class SampleEnlistment2 : IEnlistmentNotification
2{
3 public SampleEnlistment2(AssignTransactionDemo var, int newValue)
4 {
5 _var = var;
6 _oldValue = var.i;
7 _newValue = newValue;
8 }
9
10 private AssignTransactionDemo _var;
11 private int _oldValue;
12 private int _newValue;
13
14 void IEnlistmentNotification.Commit(Enlistment enlistment)
15 {
16 _var.i = _newValue;
17 Console.WriteLine("提交!i的值变为:" + _var.i.ToString());
18 enlistment.Done();
19 }
20
21 void IEnlistmentNotification.InDoubt(Enlistment enlistment)
22 {
23 throw new Exception("The method or operation is not implemented.");
24 }
25
26 void IEnlistmentNotification.Prepare(PreparingEnlistment preparingEnlistment)
27 {
28 preparingEnlistment.Prepared();
29 }
30
31 void IEnlistmentNotification.Rollback(Enlistment enlistment)
32 {
33 _var.i = _oldValue;
34 Console.WriteLine("回滚!i的值变为:" + _var.i.ToString());
35 enlistment.Done();
36 }
37}
38
39class AssignTransactionDemo
40{
41 public int i;
42
43 public void AssignIntVarValue(int newValue)
44 {
45 SampleEnlistment2 myEnlistment2 = new SampleEnlistment2(this, newValue);
46 Guid guid = new Guid("{3456789A-7654-2345-ABCD-098765434567}");
47 Transaction.Current.EnlistDurable(guid, myEnlistment2, EnlistmentOptions.None);
48 }
49}
50
51

  然后,这样来使用:

1AssignTransactionDemo atd = new AssignTransactionDemo();
2atd.i = 0;
3using (TransactionScope scope1 = new TransactionScope())
4{
5 atd.AssignIntVarValue(1);
6 Console.WriteLine("事务完成!");
7 scope1.Complete();
8 Console.WriteLine("退出区域之前,i的值为:" + atd.i.ToString());
9}
10Thread.Sleep(1000);
11Console.WriteLine("退出区域之后,i的值为:" + atd.i.ToString());

  运行这一段代码,我们可以看到如下结果:

  事务完成!
  退出区域之前,i的值为:0
  提交!i的值变为:1
  退出区域之后,i的值为:1


  从输出结果来看,赋值操作被成功执行了。可是有没有感觉有些奇怪?先做个讨论:

  1、如果前面没有 Thread.Sleep(1000) 这一行,那么我们多半会看到最后一行的输出中,i 的值依然会是 0!为什么?想想就容易明白,这里对 Commit 方法是采用的异步调用,如同另开了一个线程。如果主线程不作等待的话,当输出的时候事务的 Commit 方法多半还没有被执行,输出的结果当然就会不对。

  2、这个例子中,赋值操作是在 Commit 方法中才实际执行的。但实际上就本例而言,我们也可以做个调整:将赋值操作放在 AssignIntVarValue 方法的最后去执行,然后把 Commit 方法中的赋值操作去掉。相关的代码变化如下:

1class SampleEnlistment2 : IEnlistmentNotification
2{
3 void IEnlistmentNotification.Commit(Enlistment enlistment)
4 {
5 enlistment.Done();
6 }
7 //其它略
8}
9
10class AssignTransactionDemo
11{
12 public int i;
13
14 public void AssignIntVarValue(int newValue)
15 {
16 SampleEnlistment2 myEnlistment2 = new SampleEnlistment2(this, newValue);
17 Guid guid = new Guid("{3456789A-7654-2345-ABCD-098765434567}");
18 Transaction.Current.EnlistDurable(guid, myEnlistment2, EnlistmentOptions.None);
19 i = newValue;
20 Console.WriteLine("提交前改变!i的值为:" + i.ToString());
21 }
22}
23
24

 

[1] [2]  下一页


在百度中搜索更多在.NET 2.0 中使用自定义事务操作相关网页 转贴于:中国下载站

  • 上一篇文章:ASP.NET2.0的控件状态和视图状态探讨
  • 下一篇文章:.net下访问Access数据库需要注意的问题
  • 阅读统计:[]
  • 中国下载站】【设为主页】【收藏本页】【打印本文】【回到顶部】【关闭此页

    相关文章
    文章评论(评论内容只代表网友观点,与本站立场无关!)

    用户名: 查看更多评论

    分 值:100分 85分 70分 55分 40分 25分 10分 0分

    内 容:

             (注“”为必填内容。) 验证码: 验证码,看不清楚?请点击刷新验证码


    设为首页 - 关于我们 - 广告服务 - 网站地图 - 加入收藏 - 网站声明 - 网站帮助 - 友情链接

    • Copyright (C) 2006-2008 www.cndownz.com All Rights Reserved.
      中国下载站 版权所有. 粤ICP备05141802号. 对本站有任何建议、意见或投诉,请来信:cndownzcom@yahoo.com.cn.
      喜欢中国下载站(cndownz.com),请把中国下载站(cndownz.com)告诉你QQ上的5位好友,多谢支持!