Beispiel #1
0
 public TransactionEventInfo(PaoTransaction trans
                             , bool screenshot = false
                             , bool snapshot   = false) : base(null, trans.ToString(), screenshot, snapshot)
 {
     Transaction = (PaoTransaction)IOPublic.ObjectClone(trans);
     if (trans.Status is TransStatus_Running)
     {
         Type = EventType_TransactionRunning;
     }
     else if (trans.Status is TransStatus_Committed)
     {
         Type = EventType_TransactionCommitted;
     }
     else if (trans.Status is TransStatus_Excepted)
     {
         Type = EventType_TransactionExcepted;
         InnerExceptionInfo = new ExceptionEventInfo(trans.Status.As <TransStatus_Excepted>().Exception);
     }
     else if (trans.Status is TransStatus_Failed)
     {
         Type = EventType_TransactionFailed;
     }
     else if (trans.Status is TransStatus_Rollbacked)
     {
         Type = EventType_TransactionRollbacked;
     }
     else if (trans.Status is TransStatus_RollbackExcepted)
     {
         Type = EventType_TransactionRollbackExcepted;
         InnerExceptionInfo = new ExceptionEventInfo(trans.Status.As <TransStatus_RollbackExcepted>().Exception);
     }
 }
Beispiel #2
0
 /// <summary>
 /// 启动事务
 /// </summary>
 public void Start()
 {
     Parent    = Current;
     Current   = this;
     StartTime = DateTime.Now;
     Status    = Status.Default <TransStatus_Running>();
     TransactionPublic.ProcessTransactionEvent(this);
 }
Beispiel #3
0
        /// <summary>
        /// 在事务中执行
        /// </summary>
        /// <param name="transName">事务名称</param>
        /// <param name="exceptionAction">异常动作</param>
        /// <param name="action">动作</param>
        /// <param name="rollbackAction">回滚动作</param>
        public static void Run(string transName, Action action
                               , Action <Exception> exceptionAction = null
                               , Action rollbackAction = null)
        {
            // 先从事务列表中查找可能存在的事务
            PaoTransaction trans = new PaoTransaction()
            {
                Name = transName
            };

            try {
                trans.Start();
                if (action != null)
                {
                    action();
                }
                trans.Commit();
            }
            catch (Exception exp) {
                try {
                    trans.Exception(exp);
                    if (exceptionAction != null)
                    {
                        exceptionAction(exp);
                    }
                    else if (rollbackAction != null)
                    {
                        try {
                            rollbackAction();
                            trans.Rollback();
                        }
                        catch (Exception rollExp) {
                            trans.RollbackException(rollExp);
                        }
                    }
                    else
                    {
                        throw;
                    }
                }
                finally {
                    trans.Fail();
                }
            }
        }
Beispiel #4
0
 /// <summary>
 /// 服务端启动事务(注意:带事务的服务必须调用此方法)
 /// </summary>
 /// <example>
 /// RunService(trans, ()=> { CallSomeFunc();});
 /// </example>
 /// <param name="clientTrans">客户端事务</param>
 /// <param name="transName">事务名称</param>
 /// <param name="exceptionAction">异常动作</param>
 /// <param name="action">动作</param>
 /// <param name="rollbackAction">回滚动作</param>
 public static void RunService(PaoTransaction clientTrans
                               , string transName
                               , Action action
                               , Action <Exception> exceptionAction = null
                               , Action rollbackAction = null)
 {
     // 将客户端事务插入当前事务中
     if (PaoTransaction.Current != null)
     {
         clientTrans.Parent = PaoTransaction.Current.Parent;
     }
     PaoTransaction.Current = clientTrans;
     try {
         // 运行事务
         Run(transName, action, exceptionAction, rollbackAction);
     }
     finally {
         // 运行完成后恢复原事务
         PaoTransaction.Current = PaoTransaction.Current.Parent;
     }
 }
Beispiel #5
0
 public static void ProcessTransactionEvent(PaoTransaction trans)
 {
     EventPublic.FireEvent(new TransactionEventInfo(trans));
 }
Beispiel #6
0
 /// <summary>
 /// 结束事务
 /// </summary>
 private void End()
 {
     Current = Parent;
     Parent  = null;
 }