Exemple #1
0
        private TccRunningContext <TContext> GetRunningContext()
        {
            TccRunningContext <TContext> rc = new TccRunningContext <TContext>(_context, _tracing);
            TccTransactionState          nextState;

            switch (_txState)
            {
            case TccTransactionState.LockTrying:
                rc.AddWork(_lock, TccAction.Try);
                rc.SetNextState(TccTransactionState.Trying, TccTransactionState.Cancelling);
                break;

            case TccTransactionState.Trying:
                bool hasNext = false;
                foreach (var a in _works)
                {
                    if (a.WorkState != TccWorkState.Tryed)
                    {
                        if (a.CanTry())
                        {
                            rc.AddWork(a, TccAction.Try);
                        }
                        else
                        {
                            hasNext = true;
                        }
                    }
                }
                nextState = hasNext ? TccTransactionState.Trying : TccTransactionState.Confirming;
                rc.SetNextState(nextState, TccTransactionState.Cancelling);
                break;

            case TccTransactionState.Confirming:
                foreach (var a in _works)
                {
                    if (a.WorkState != TccWorkState.Confirmed)
                    {
                        rc.AddWork(a, TccAction.Confirm);
                    }
                }
                nextState = _lock != null ? TccTransactionState.LockConfirming :TccTransactionState.Confirmed;
                rc.SetNextState(nextState, TccTransactionState.ConfirmFailed);
                break;

            case TccTransactionState.LockConfirming:
                rc.AddWork(_lock, TccAction.Confirm);
                rc.SetNextState(TccTransactionState.Confirmed, TccTransactionState.ConfirmFailed);
                break;

            case TccTransactionState.Cancelling:
                foreach (var a in _works)
                {
                    if (a.WorkState != TccWorkState.Cancelled && a.WorkState != TccWorkState.None)
                    {
                        rc.AddWork(a, TccAction.Cancel);
                    }
                }
                nextState = _lock != null ? TccTransactionState.LockCanceling :TccTransactionState.Cancelled;
                rc.SetNextState(nextState, TccTransactionState.CancelFailed);
                break;

            case TccTransactionState.LockCanceling:
                rc.AddWork(_lock, TccAction.Cancel);
                rc.SetNextState(TccTransactionState.Cancelled, TccTransactionState.CancelFailed);
                break;
            }
            return(rc);
        }