增加CheckConnectionExecuting和CheckConnectionExecuted事件,用于监听数据库连接事件,方便排查是否是因为连接池紧缺导致的连接速度慢

This commit is contained in:
guoshun.du 2024-12-31 14:52:57 +08:00
parent 7aa10044aa
commit 1b261b3177
7 changed files with 59 additions and 12 deletions

View File

@ -599,6 +599,8 @@ namespace SqlSugar.TDSQLForPGODBC
OnLogExecuted = it.AopEvents?.OnLogExecuted,
OnLogExecuting = it.AopEvents?.OnLogExecuting,
DataExecuted = it.AopEvents?.DataExecuted,
CheckConnectionExecuted = it.AopEvents?.CheckConnectionExecuted,
CheckConnectionExecuting = it.AopEvents?.CheckConnectionExecuting,
},
ConfigId = it.ConfigId,
ConfigureExternalServices = it.ConfigureExternalServices == null ? null : new ConfigureExternalServices()

View File

@ -39,6 +39,7 @@ namespace SqlSugar
internal bool OldClearParameters { get; set; }
public IDataParameterCollection DataReaderParameters { get; set; }
public TimeSpan SqlExecutionTime { get { return AfterTime - BeforeTime; } }
public TimeSpan ConnectionExecutionTime { get { return CheckConnectionAfterTime - CheckConnectionBeforeTime; } }
/// <summary>
/// Add, delete and modify: the number of affected items;
/// </summary>
@ -47,6 +48,8 @@ namespace SqlSugar
public bool IsDisableMasterSlaveSeparation { get; set; }
internal DateTime BeforeTime = DateTime.MinValue;
internal DateTime AfterTime = DateTime.MinValue;
internal DateTime CheckConnectionBeforeTime = DateTime.MinValue;
internal DateTime CheckConnectionAfterTime = DateTime.MinValue;
public virtual IDbBind DbBind
{
get
@ -66,6 +69,8 @@ namespace SqlSugar
public virtual bool IsClearParameters { get; set; }
public virtual Action<string, SugarParameter[]> LogEventStarting => this.Context.CurrentConnectionConfig.AopEvents?.OnLogExecuting;
public virtual Action<string, SugarParameter[]> LogEventCompleted => this.Context.CurrentConnectionConfig.AopEvents?.OnLogExecuted;
public virtual Action<IDbConnection> CheckConnectionExecuting => this.Context.CurrentConnectionConfig.AopEvents?.CheckConnectionExecuting;
public virtual Action<IDbConnection, TimeSpan> CheckConnectionExecuted => this.Context.CurrentConnectionConfig.AopEvents?.CheckConnectionExecuted;
public virtual Func<string, SugarParameter[], KeyValuePair<string, SugarParameter[]>> ProcessingEventStartingSQL => this.Context.CurrentConnectionConfig.AopEvents?.OnExecutingChangeSql;
protected virtual Func<string, string> FormatSql { get; set; }
public virtual Action<SqlSugarException> ErrorEvent => this.Context.CurrentConnectionConfig.AopEvents?.OnError;
@ -175,6 +180,7 @@ namespace SqlSugar
}
public virtual void CheckConnection()
{
this.CheckConnectionBefore(this.Connection);
if (this.Connection.State != ConnectionState.Open)
{
try
@ -190,10 +196,12 @@ namespace SqlSugar
Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message+$"DbType=\"{this.Context.CurrentConnectionConfig.DbType}\";ConfigId=\"{this.Context.CurrentConnectionConfig.ConfigId}\"");
}
}
this.CheckConnectionAfter(this.Connection);
}
public virtual async Task CheckConnectionAsync()
{
this.CheckConnectionBefore(this.Connection);
if (this.Connection.State != ConnectionState.Open)
{
try
@ -205,6 +213,31 @@ namespace SqlSugar
Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message + $"DbType=\"{this.Context.CurrentConnectionConfig.DbType}\";ConfigId=\"{this.Context.CurrentConnectionConfig.ConfigId}\"");
}
}
this.CheckConnectionAfter(this.Connection);
}
public virtual void CheckConnectionBefore(IDbConnection Connection)
{
this.CheckConnectionBeforeTime = DateTime.Now;
if (this.IsEnableLogEvent)
{
Action<IDbConnection> action = CheckConnectionExecuting;
if (action != null)
{
action(Connection);
}
}
}
public virtual void CheckConnectionAfter(IDbConnection Connection)
{
this.CheckConnectionAfterTime = DateTime.Now;
if (this.IsEnableLogEvent)
{
Action<IDbConnection, TimeSpan> action = CheckConnectionExecuted;
if (action != null)
{
action(Connection,this.ConnectionExecutionTime);
}
}
}
#endregion

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
@ -22,5 +23,7 @@ namespace SqlSugar
public virtual Action<object, DataFilterModel> DataExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuting = value; } }
public Action<object, DataFilterModel> DataChangesExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.DataChangesExecuted = value; } }
public virtual Action<object, DataAfterModel> DataExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.DataExecuted = value; } }
public Action<IDbConnection> CheckConnectionExecuting { set { this.Context.CurrentConnectionConfig.AopEvents.CheckConnectionExecuting = value; } }
public Action<IDbConnection, TimeSpan> CheckConnectionExecuted { set { this.Context.CurrentConnectionConfig.AopEvents.CheckConnectionExecuted = value; } }
}
}

View File

@ -94,6 +94,8 @@ namespace SqlSugar
public Action<object, DataFilterModel> DataExecuting { get; set; }
public Action<object, DataFilterModel> DataChangesExecuted { get; set; }
public Action<object, DataAfterModel> DataExecuted { get; set; }
public Action<IDbConnection> CheckConnectionExecuting { get; set; }
public Action<IDbConnection,TimeSpan> CheckConnectionExecuted { get; set; }
}
public class ConfigureExternalServices
{

View File

@ -18,11 +18,13 @@ namespace SqlSugar
IDataParameter[] ToIDbDataParameter(params SugarParameter[] pars);
SugarParameter[] GetParameters(object obj, PropertyInfo[] propertyInfo = null);
SqlSugarProvider Context { get; set; }
void CheckConnectionAfter(IDbConnection Connection);
void CheckConnectionBefore(IDbConnection Connection);
void ExecuteBefore(string sql, SugarParameter[] pars);
void ExecuteAfter(string sql, SugarParameter[] pars);
bool IsAnyTran();
bool IsNoTran();
bool IsEnableLogEvent{get;set;}
bool IsEnableLogEvent { get; set; }
StackTraceInfo SqlStackTrace { get; }
IDataParameterCollection DataReaderParameters { get; set; }
CommandType CommandType { get; set; }
@ -31,6 +33,7 @@ namespace SqlSugar
bool IsClearParameters { get; set; }
int CommandTimeOut { get; set; }
TimeSpan SqlExecutionTime { get; }
TimeSpan ConnectionExecutionTime { get; }
int SqlExecuteCount { get; }
IDbBind DbBind { get; }
void SetCommandToAdapter(IDataAdapter adapter, DbCommand command);
@ -79,7 +82,7 @@ namespace SqlSugar
Task<int> ExecuteCommandAsync(string sql, params SugarParameter[] parameters);
Task<int> ExecuteCommandAsync(string sql, object parameters);
Task<int> ExecuteCommandAsync(string sql, object parameters,CancellationToken cancellationToken);
Task<int> ExecuteCommandAsync(string sql, object parameters, CancellationToken cancellationToken);
Task<int> ExecuteCommandAsync(string sql, List<SugarParameter> parameters);
string GetString(string sql, object parameters);
@ -99,9 +102,9 @@ namespace SqlSugar
Task<int> GetIntAsync(string sql, List<SugarParameter> parameters);
long GetLong(string sql, object pars=null);
long GetLong(string sql, object pars = null);
Task<long> GetLongAsync(string sql, object pars=null);
Task<long> GetLongAsync(string sql, object pars = null);
Double GetDouble(string sql, object parameters);
@ -132,12 +135,12 @@ namespace SqlSugar
Task<DateTime> GetDateTimeAsync(string sql, List<SugarParameter> parameters);
Tuple<List<T>, List<T2>> SqlQuery<T,T2>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>> SqlQuery<T, T2,T3>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>,List<T4>> SqlQuery<T,T2,T3,T4>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>> SqlQuery<T, T2, T3, T4,T5>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>> SqlQuery<T, T2, T3, T4, T5,T6>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>, List<T7>> SqlQuery<T, T2, T3, T4, T5, T6,T7>(string sql, object parameters = null);
Tuple<List<T>, List<T2>> SqlQuery<T, T2>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>> SqlQuery<T, T2, T3>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>> SqlQuery<T, T2, T3, T4>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>> SqlQuery<T, T2, T3, T4, T5>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>> SqlQuery<T, T2, T3, T4, T5, T6>(string sql, object parameters = null);
Tuple<List<T>, List<T2>, List<T3>, List<T4>, List<T5>, List<T6>, List<T7>> SqlQuery<T, T2, T3, T4, T5, T6, T7>(string sql, object parameters = null);
Task<Tuple<List<T>, List<T2>>> SqlQueryAsync<T, T2>(string sql, object parameters = null);
Task<Tuple<List<T>, List<T2>, List<T3>>> SqlQueryAsync<T, T2, T3>(string sql, object parameters = null);
@ -153,7 +156,7 @@ namespace SqlSugar
List<T> MasterSqlQuery<T>(string sql, object parameters = null);
Task<List<T>> SqlQueryAsync<T>(string sql, object parameters = null);
Task<List<T>> SqlQueryAsync<T>(string sql, object parameters,CancellationToken token);
Task<List<T>> SqlQueryAsync<T>(string sql, object parameters, CancellationToken token);
Task<List<T>> SqlQueryAsync<T>(string sql, List<SugarParameter> parameters);
Task<List<T>> SqlQueryAsync<T>(string sql, params SugarParameter[] parameters);
@ -191,6 +194,6 @@ namespace SqlSugar
Task<DbResult<bool>> UseTranAsync(Func<Task> action, Action<Exception> errorCallBack = null);
Task<DbResult<T>> UseTranAsync<T>(Func<Task<T>> action, Action<Exception> errorCallBack = null);
IAdo UseStoredProcedure();
}
}

View File

@ -80,6 +80,7 @@ namespace SqlSugar
/// </summary>
public override void CheckConnection()
{
this.CheckConnectionBefore(this.Connection);
if (this.Connection.State != ConnectionState.Open)
{
try
@ -104,6 +105,7 @@ namespace SqlSugar
Check.Exception(true, ErrorMessage.ConnnectionOpen, ex.Message);
}
}
this.CheckConnectionAfter(this.Connection);
}
public override void SetCommandToAdapter(IDataAdapter dataAdapter, DbCommand command)
{

View File

@ -662,6 +662,8 @@ namespace SqlSugar
OnLogExecuted=it.AopEvents?.OnLogExecuted,
OnLogExecuting= it.AopEvents?.OnLogExecuting,
DataExecuted = it.AopEvents?.DataExecuted,
CheckConnectionExecuted = it.AopEvents?.CheckConnectionExecuted,
CheckConnectionExecuting = it.AopEvents?.CheckConnectionExecuting,
},
ConfigId = it.ConfigId,
ConfigureExternalServices =it.ConfigureExternalServices==null?null:new ConfigureExternalServices() {