volatilerm.cs

来自「< SQL Server2005程序设计>」· CS 代码 · 共 67 行

CS
67
字号
using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;

namespace TheRM
{
    public class VolatileRM : IEnlistmentNotification
    {
        private string whoAmI = "";
        public VolatileRM(string WhoAmI)
        {
            whoAmI = WhoAmI;
        }

        private int memberValue = 0;
        private int oldMemberValue = 0;
        public int MemberValue
        {
            get { return memberValue; }
            set
            {
                Transaction currentTx = Transaction.Current;
                if (currentTx != null)
                {
                    Console.WriteLine(whoAmI + ": MemberValue setter - EnlistVolatile");
                    currentTx.EnlistVolatile(this, EnlistmentOptions.None);
                }
                oldMemberValue = memberValue;
                memberValue = value;
            }
        }

        #region IEnlistmentNotification Members
        public void Commit(Enlistment enlistment)
        {
            Console.WriteLine(whoAmI + ": Commit");
            // Clear out oldMemberValue
            oldMemberValue = 0;
            enlistment.Done();
        }

        public void InDoubt(Enlistment enlistment)
        {
            Console.WriteLine(whoAmI + ": InDoubt");
            enlistment.Done();
        }

        public void Prepare(PreparingEnlistment preparingEnlistment)
        {
            Console.WriteLine(whoAmI + ": Prepare");
//            preparingEnlistment.Prepared();
            preparingEnlistment.ForceRollback();
        }

        public void Rollback(Enlistment enlistment)
        {
            Console.WriteLine(whoAmI + ": Rollback");
            // Restore previous state
            memberValue = oldMemberValue;
            oldMemberValue = 0;
            enlistment.Done();
        }
        #endregion
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?