/// <inheritdoc />
        public void PerformChanges(Func <bool> changes)
        {
            void InternalAction()
            {
                BeginChanges();

                try
                {
                    if (changes.Invoke())
                    {
                        SaveChanges();
                    }
                    else
                    {
                        CancelChanges();
                    }
                }
                catch (Exception exception)
                {
                    CancelChanges();
                    ExceptionDispatchInfo.Capture(exception).Throw();
                }
            }

            Retrier.InvokeAction(
                InternalAction,
                Settings.Instance.DBContextSetting.RetriesNumber,
                Settings.Instance.DBContextSetting.SleepBetweenRetriesMilliSeconds,
                RetrierHelper.IsRetriableError);
        }
Ejemplo n.º 2
0
        private void ExecuteCommand(
            string storedProcedureName,
            DbParameter[] parameters,
            Action <DbCommand> action,
            bool closeConnection)
        {
            void InternalAction()
            {
                DbCommand command = CreateCommand(storedProcedureName, parameters);

                try
                {
                    if (_connection.State != ConnectionState.Open)
                    {
                        _connection.Open();
                    }

                    DateTime startUtc = DateTime.UtcNow;
                    action.Invoke(command);
                    var executionTimeSeconds = (int)DateTime.UtcNow.Subtract(startUtc).TotalSeconds;

                    if (executionTimeSeconds >= Settings.Instance.DBContextSetting.LongRunningStoredProcedureTimeSeconds)
                    {
                        OnLongRunningStoredProcedure?.Invoke(
                            this,
                            new LongRunningStoredProcedureEventArgs(storedProcedureName, executionTimeSeconds));
                    }
                }
                catch (Exception exception)
                {
                    //DbConnection.ClearPool(_connection);
                    ExceptionDispatchInfo.Capture(exception).Throw();
                }
                finally
                {
                    if (!InTransaction() && closeConnection)
                    {
                        DisposeConnection();
                    }
                }
            }

            if (InTransaction())
            {
                InternalAction();
            }
            else
            {
                Retrier.InvokeAction(
                    InternalAction,
                    Settings.Instance.DBContextSetting.RetriesNumber,
                    Settings.Instance.DBContextSetting.SleepBetweenRetriesMilliSeconds,
                    RetrierHelper.IsRetriableError);
            }
        }