增加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,6 +18,8 @@ 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();
@ -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);

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() {