コード例 #1
0
        public TccTransaction(TccTransactionData data, TccCoordinator <TContext> coordinator, TContext ctx)
        {
            // TODO: 从TransactionData中恢复数据
            _context = ctx.Deserialize <TContext>(data.ContextData);
            _txId    = data.TxId;
            _txState = data.TxState;

            _lock  = null;
            _works = new List <TccWorkUnit <TContext> >();
            _txCtx = null;

            _coordinator = coordinator;
            if (_coordinator != null)
            {
                _coordinator.NewTransaction(this);
                _tracing = _coordinator.Tracing;
            }
            else
            {
                _tracing = _defaultTracing;
            }

            Prepare();

            foreach (var a in _works)
            {
                a.Tracing = _tracing;
            }
        }
コード例 #2
0
        public TccTransaction(TContext ctx, TccCoordinator <TContext> coordinator)
        {
            _context = ctx;
            _txId    = Guid.NewGuid().ToString();
            _txState = TccTransactionState.New;

            _lock  = null;
            _works = new List <TccWorkUnit <TContext> >();
            _txCtx = null;

            Prepare();
            _coordinator = coordinator;
            if (_coordinator != null)
            {
                _coordinator.NewTransaction(this);
                _tracing = _coordinator.Tracing;
            }
            else
            {
                _tracing = _defaultTracing;
            }

            foreach (var a in _works)
            {
                a.Tracing = _tracing;
            }
        }
コード例 #3
0
        private void CheckAndRunNext(TccRunningContext <TContext> rc)
        {
            if (rc != null)
            {
                _txState = rc.NextState;
                if (_txState == TccTransactionState.Confirmed)
                {
                    if (_coordinator != null)
                    {
                        _coordinator.CloseTransaction(this);
                    }
                    _txCtx.Return();
                    return;
                }
                else if (_txState == TccTransactionState.Cancelled)
                {
                    if (_coordinator != null)
                    {
                        _coordinator.CloseTransaction(this);
                    }
                    _txCtx.ThrowException("Transaction Cancelled", GetFirstError());
                    return;
                }
                else if (_txState == TccTransactionState.ConfirmFailed || _txState == TccTransactionState.CancelFailed)
                {
                    if (_coordinator != null)
                    {
                        _coordinator.CloseTransaction(this);
                    }
                    _txCtx.ThrowException("Transaction Failed", GetFirstError());
                    return;
                }
                else
                {
                    if (_coordinator != null)
                    {
                        _coordinator.UpdateTransaction(this);
                    }
                }
            }
            lock (_syncRoot) {
                rc = GetRunningContext();
            }

            _tracing.InfoFmt("NextRunningContext() = {0}", rc);
            rc.Run(
                delegate() {
                try {
                    CheckAndRunNext(rc);
                } catch (Exception ex) {
                    _tracing.Error(ex, "CheckAndRunNext() Failed");
                    _txCtx.ThrowException("TccTransaction.CheckAndRunNext() Failed", ex);
                }
            }
                );
        }
コード例 #4
0
        public void BeginExecute(Action <TccTransactionContext <TContext> > callback)
        {
            lock (_syncRoot) {
                if (_txCtx != null)
                {
                    throw new NotSupportedException("[BUG] BeginInvoke for every TccTransaction can only run once");
                }

                _txCtx     = new TccTransactionContext <TContext>(this, callback);
                _txState   = _lock != null ? TccTransactionState.LockTrying : TccTransactionState.Trying;
                _beginTime = DateTime.Now;
            }

            try {
                CheckAndRunNext(null);
            } catch (Exception ex) {
                _txCtx.ThrowException("[BUG] BeginExecute CheckAndRunNext Failed", ex);
            }
        }
コード例 #5
0
        public void BeginReclose(Action <TccTransactionContext <TContext> > callback)
        {
            //
            // TODO:尝试重新关闭Transaction,当Confirm或者Cancel Failed
            //
            // ?: 当从Persister当中序列化出来的时候,应当怎样去操作
            //
            // Confirming & ConfirmFailed   :  继续confirm
            // Cancelling & cancelFailed    :  继续cancel
            // Trying                       :  cancel
            //
            if (_txCtx != null)
            {
                throw new NotSupportedException("TccTransactionContext is not Null");
            }
            _txCtx = new TccTransactionContext <TContext>(this, callback);
            lock (_syncRoot)
            {
                if (_txState == TccTransactionState.CancelFailed || _txState == TccTransactionState.Cancelling || _txState == TccTransactionState.Trying)
                {
                    _txState = TccTransactionState.Cancelling;
                }
                else if (_txState == TccTransactionState.ConfirmFailed || _txState == TccTransactionState.Confirming)
                {
                    _txState = TccTransactionState.Confirming;
                }
                else
                {
                    throw new NotSupportedException("TccTransactionState can not Reclose:" + _txState.ToString());
                }
            }

            try
            {
                CheckAndRunNext(null);
            }
            catch (Exception ex)
            {
                _txCtx.ThrowException("[BUG] BeginClose CheckAndRunNext Failed", ex);
            }
        }
コード例 #6
0
 public void SetNextState(TccTransactionState succState, TccTransactionState failedState)
 {
     _succState   = succState;
     _failedState = failedState;
 }