/// <summary>
        /// 在事物中执行一个不需要返回结果的函数,该函数可接收一个已初始化的 IEntityContext 参数。
        /// 如果在执行过程中发生并发冲突,则会额外进行 concurrencyRetryCount 参数指定次数的重试,默认执行 1 次重试。
        /// </summary>
        /// <param name="action">要在事物中执行的函数。</param>
        /// <param name="concurrencyRetryCount">并发重试次数。</param>
        public static void ExecuteInTransition(Action <IEntityContext> action, int concurrencyRetryCount = 1)
        {
            int retryCount = 0;

            while (true)
            {
                using (IEntityContext entityContext = EntityContextFactory.CreateEntityContext())
                {
                    try
                    {
                        entityContext.BeginTransaction();

                        action(entityContext);

                        entityContext.Commit();

                        break;
                    }
                    catch (EntityConcurrencyException)
                    {
                        entityContext.Rollback();

                        retryCount++;

                        if (retryCount > concurrencyRetryCount)
                        {
                            throw;
                        }

                        continue;
                    }
                    catch
                    {
                        entityContext.Rollback();

                        throw;
                    }
                }
            }
        }
        /// <summary>
        /// 在事物中执行一个需要返回结果的函数,该函数可接收一个已初始化的 IEntityContext 参数。
        /// 如果在执行过程中发生并发冲突,则会额外进行 concurrencyRetryCount 参数指定次数的重试,默认执行 1 次重试。
        /// </summary>
        /// <typeparam name="TResult">函数执行结果的类型。</typeparam>
        /// <param name="func">要在事物中执行的函数。</param>
        /// <param name="concurrencyRetryCount">并发重试次数。</param>
        /// <returns>函数执行的结果。</returns>
        public static TResult ExecuteInTransition <TResult>(Func <IEntityContext, TResult> func, int concurrencyRetryCount = 1)
        {
            int retryCount = 0;

            while (true)
            {
                using (IEntityContext entityContext = EntityContextFactory.CreateEntityContext())
                {
                    try
                    {
                        entityContext.BeginTransaction();

                        var result = func(entityContext);

                        entityContext.Commit();

                        return(result);
                    }
                    catch (EntityConcurrencyException)
                    {
                        entityContext.Rollback();

                        retryCount++;

                        if (retryCount > concurrencyRetryCount)
                        {
                            throw;
                        }

                        continue;
                    }
                    catch
                    {
                        entityContext.Rollback();

                        throw;
                    }
                }
            }
        }
Exemple #3
0
 public void BeginTransaction()
 {
     _context.BeginTransaction();
 }