/// <inheritdoc /> public IRepositoryContext CreateRepositoryContext() { var repositoryContext = new RepositoryContext(); repositoryContext.OnLongRunningStoredProcedure += (s, e) => OnLongRunningStoredProcedure?.Invoke( this, new LongRunningStoredProcedureEventArgs(e.StoredProcedureName, e.ExecutionTimeSeconds)); return(repositoryContext); }
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); } }
private UserContext GetUserContext(DBType dbType) { var dbOrdinal = (int)dbType; if (_userContexts[dbOrdinal] == null) { var userContext = _userContexts[(int)dbType]; userContext.OnLongRunningStoredProcedure += (s, e) => OnLongRunningStoredProcedure?.Invoke( this, new LongRunningStoredProcedureEventArgs(e.StoredProcedureName, e.ExecutionTimeSeconds)); _userContexts[dbOrdinal] = userContext; if (_inChanges) { _userContexts[dbOrdinal]?.Database.BeginTransaction(); } } return(_userContexts[dbOrdinal]); }