在组件之间实现事务和异步提交事务(NET2.0)

假设要Insert1个用户和该用户对应角色,而这2个Insert方法已经封装到2个dll中了,现在要在这个2个方法中实现事务,代码很简单,如下(需先添加对System.Transactions程序集的引用)

 1 None.gif public   void  Insert( string  UserName,ArrayList alRoles)
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gifusing (TransactionScope _ts = new TransactionScope())
 4ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
 5InBlock.gif     //插入用户
 6InBlock.gif      new user().Insert(UserName);
 7InBlock.gif
 8InBlock.gif     //  插入角色
 9InBlock.gif       new Role().Insert(UserName,alRoles);
10InBlock.gif
11InBlock.gif      _ts.Complete();
12ExpandedSubBlockEnd.gif}

13ExpandedBlockEnd.gif}

异步提交事务的代码也很简单:

 1 None.gif          public   void  Work()
 2 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
 3InBlock.gif            Transaction _old = Transaction.Current;
 4InBlock.gif            CommittableTransaction _newCommit = new CommittableTransaction();
 5InBlock.gif            Transaction.Current = _newCommit;
 6InBlock.gif
 7InBlock.gif            try
 8ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
 9InBlock.gif                _newCommit.BeginCommit(OnCommit, null);
10ExpandedSubBlockEnd.gif            }

11InBlock.gif            finally
12ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
13InBlock.gif                Transaction.Current = _old;
14ExpandedSubBlockEnd.gif            }

15ExpandedBlockEnd.gif        }

16 None.gif         void  OnCommit(IAsyncResult asy)
17 ExpandedBlockStart.gifContractedBlock.gif         dot.gif {
18InBlock.gif            CommittableTransaction _commit;
19InBlock.gif            _commit = asy as CommittableTransaction;
20InBlock.gif
21InBlock.gif            try
22ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
23InBlock.gif                using (_commit)
24ExpandedSubBlockStart.gifContractedSubBlock.gif                dot.gif{
25InBlock.gif                    SqlConnection _cnn = new SqlConnection("dot.gif");
26InBlock.gif                    SqlCommand _cmd = new SqlCommand();
27InBlock.gif
28InBlock.gif
29InBlock.gif                    _cnn.Open();
30InBlock.gif
31InBlock.gif                    _cnn.EnlistTransaction(_commit);    //  利用事务
32InBlock.gif
33InBlock.gif                    _cmd.Connection = _cnn;
34InBlock.gif
35InBlock.gif                    _cmd.CommandText = "dot.gif.";
36InBlock.gif                    _cmd.ExecuteNonQuery();
37InBlock.gif
38InBlock.gif                    _cmd.CommandText = "dot.gif";
39InBlock.gif                    _cmd.ExecuteNonQuery();
40InBlock.gif
41InBlock.gif                    _commit.EndCommit(asy);
42InBlock.gif
43InBlock.gif                    _cnn.Close();
44ExpandedSubBlockEnd.gif                }

45ExpandedSubBlockEnd.gif            }

46InBlock.gif            catch (Exception e)
47ExpandedSubBlockStart.gifContractedSubBlock.gif            dot.gif{
48InBlock.gif
49ExpandedSubBlockEnd.gif            }

50ExpandedBlockEnd.gif        }

上面我们用TransactionScope和CommittableTransaction。
TransactionScope是隐示的,是由系统自动管理的,所以它没有什么RollBack方法。
CommittableTransaction是显示的,它有commit和RollBack方法。
------------------------------------------
以上都是个人的看法和体会,如有不妥处,还请大家多多指点,谢谢!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值