예제 #1
0
 public Tuple <int, string> GetAutoNum(string branchCode, int billType)
 {
     try
     {
         _Conn.BeginTrans();
         Increase(branchCode, billType);
         int    newId     = GetInterId(branchCode, billType);
         string newBillNo = GetBillNo(branchCode, billType);
         _Conn.Commit();
         return(new Tuple <int, string>(newId, newBillNo));
     }
     catch (Exception ex)
     {
         if (_Conn.IsInTransaction)
         {
             _Conn.Rollback();
         }
         throw ex;
     }
 }
예제 #2
0
        /// <summary>
        /// 运行事务
        /// aftTransactionSuccessHandler如果出错永远不会抛出
        /// befTransactionHandler失败就直接退出
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connInfo"></param>
        /// <param name="handler"></param>
        /// <param name="successValue"></param>
        /// <param name="throwIfError"></param>
        /// <returns></returns>
        public static ResultExchange <T> RunTransactionWithResult <T>(
            this DbContext connInfo,
            Func <DbContext, T> handler, bool throwIfError = false,
            Action aftTransactionSuccessHandler            = null,
            Action <Exception> errorHandler = null,
            Action befTransactionHandler    = null,
            IsolationLevel?isolation        = null)
        {
            ResultExchange <T> result = new ResultExchange <T>();

            try
            {
                befTransactionHandler?.Invoke();
            }
            catch (Exception ex)
            {
                ex.ExpToResult(result);
                errorHandler?.Invoke(ex);
                return(result);
            }
            try
            {
                if (isolation.HasValue)
                {
                    connInfo.BeginTrans(isolation.Value);
                }
                else
                {
                    connInfo.BeginTrans();
                }
                result.data = handler(connInfo);
                if (null != result && result.Success)
                {
                    connInfo.Commit();
                    try //不应该抛出错误
                    {
                        aftTransactionSuccessHandler?.Invoke();
                    }
                    catch (Exception ex)
                    {
                        errorHandler?.Invoke(ex);
                    }
                }
                else
                {
                    connInfo.Rollback();
                }
            }
            catch (Exception ex)
            {
                ex.ExpToResult(result);
                if (connInfo.IsInTransaction)
                {
                    connInfo.Rollback();
                }
                errorHandler?.Invoke(ex);
                if (throwIfError)
                {
                    throw ex;
                }
            }
            return(result);
        }
예제 #3
0
        /// <summary>
        /// handlerSuccessFillValue: 把handler的返回值object 写到 T返回值中
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="connInfo"></param>
        /// <param name="handler"></param>
        /// <param name="aftTransactionSuccessHandler"></param>
        /// <param name="errorHandler"></param>
        /// <param name="befTransactionHandler"></param>
        /// <param name="handlerSuccessFillValue"></param>
        /// <returns></returns>
        public static T RunTransactionFillValue <T>(this DbContext connInfo,
                                                    Func <DbContext, Tuple <bool, object> > handler, bool throwIfError = false,
                                                    Action aftTransactionSuccessHandler        = null,
                                                    Action <Exception> errorHandler            = null,
                                                    Action befTransactionHandler               = null,
                                                    Action <T, object> handlerSuccessFillValue = null,
                                                    IsolationLevel?isolation = null)
            where T : ResultBase, new()
        {
            T result = new T {
                errorCode = -1, errorMessage = "fail"
            };

            try
            {
                befTransactionHandler?.Invoke();
            }
            catch (Exception ex)
            {
                ex.ExpToResult(result);
                errorHandler?.Invoke(ex);
                return(result);
            }
            try
            {
                if (isolation.HasValue)
                {
                    connInfo.BeginTrans(isolation.Value);
                }
                else
                {
                    connInfo.BeginTrans();
                }
                Tuple <bool, object> tempRet = handler(connInfo);
                if (tempRet?.Item1 ?? false)
                {
                    connInfo.Commit();
                    result.errorCode    = 0;
                    result.errorMessage = "success";
                    try //不应该抛出错误
                    {
                        handlerSuccessFillValue?.Invoke(result, tempRet.Item2);
                        aftTransactionSuccessHandler?.Invoke();
                    }
                    catch (Exception ex)
                    {
                        errorHandler?.Invoke(ex);
                    }
                }
                else
                {
                    connInfo.Rollback();
                }
            }
            catch (Exception ex)
            {
                if (connInfo.IsInTransaction)
                {
                    connInfo.Rollback();
                }
                errorHandler?.Invoke(ex);
                if (throwIfError)
                {
                    throw ex;
                }
                ex.ExpToResult(result);
            }
            return(result);
        }