wxwinter代码空间

兰竹菊梅★春夏秋冬☆

首页 联系 订阅 管理
class TS_test : System.Transactions.IEnlistmentNotification
    {
        void System.Transactions.IEnlistmentNotification.Commit(System.Transactions.Enlistment enlistment)
            {
                Console.WriteLine("自定义事物提交");
                enlistment.Done();
            }
        void System.Transactions.IEnlistmentNotification.InDoubt(System.Transactions.Enlistment enlistment)
            {
                throw new Exception("The method or operation is not implemented.");
            }
    void System.Transactions.IEnlistmentNotification.Prepare(System.Transactions.PreparingEnlistment preparingEnlistment)
            {
                  throw new Exception("自定义事物操作没有被执行.");
                 preparingEnlistment.Prepared();
            }
        void System.Transactions.IEnlistmentNotification.Rollback(System.Transactions.Enlistment enlistment)
            {
                Console.WriteLine("自定义事物回滚");
                enlistment.Done();
             }
         }//TS_test类
       void y() //执行
        {
            using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
            {
//向事务管理器进行注册,把自定义事务加入到当前事务中去
                TS_test obj = new TS_test();
  System.Transactions.Transaction.Current.EnlistVolatile(obj, System.Transactions.EnlistmentOptions.None);
               
               
                ts.Complete();
                //执行这一段代码,我们可以得到以下的输出:
                //准备!
             //提交!
                //
                //而如果将前面的ts.Complete() 行注释掉,显然执行结果就将变为:
                //回滚!
                //
                //////////////////说明//////////////////////////////
                //当调用ts.Complete() 方法的时候,表示事务已成功执行。
                //随后,事务管理器就会寻找当前所有已注册的条目,
                //也就是IEnlistmentNotification 的每一个实现,
                //依次调用它们的Prepare 方法,即通知每个条目做好提交准备,
                //当所有条目都调用了Prepared() 表示自己已经准备妥当之后,
                //再依次调用它们的Commit 方法进行提交。
                //如果其中有一个没有调用Prepared 而是调用了ForceRollback 的话,整个事务都将回滚,
                //此时事务管理器再调用每个条目的Rollback 方法
            }
           
        }//y方法
posted on 2006-09-23 23:43  wxwinter  阅读(361)  评论(0)    收藏  举报