我正在尝试找出导致池大小达到最大连接的原因,我想验证它是否为泄漏。
我验证了我所有的连接都被using
块包裹了,例如:
public static async Task<Int32> ExecuteNonQueryAsync(String commandText, params MySqlParameter[] parameters)
{
using (MySqlConnection conn = new MySqlConnection(m_ConnectionString))
{
using (MySqlCommand cmd = new MySqlCommand(commandText, conn))
{
cmd.CommandType = CommandType.Text;
if (parameters.Length > 0)
{
cmd.Parameters.AddRange(parameters);
}
conn.Open();
return await cmd.ExecuteNonQueryAsync();
}
}
}
我添加了一个诊断日志,以防达到最大池大小:
public static void PrintPoolSize()
{
try
{
Assembly ms = Assembly.LoadFrom(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MySql.Data.dll"));// Assembly.LoadFrom("MySql.Data.dll");
Type type = ms.GetType("MySql.Data.MySqlClient.MySqlPoolManager");
MethodInfo mi = type.GetMethod("GetPool", BindingFlags.Static | BindingFlags.Public);
var pool = mi.Invoke(null, new object[] { new MySqlConnectionStringBuilder(strConnectionString) });
PropertyInfo pInfo = pool.GetType().GetProperty("NumConnections",
System.Reflection.BindingFlags.Public |
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance);
var poolNumConnectionsCount = pInfo.GetValue(pool, null);
var maxPoolSize = pool.GetType().GetFields(
BindingFlags.NonPublic |
BindingFlags.Instance).Where(i => i.Name == "maxSize").Select(i => i.GetValue(pool)).ToList().FirstOrDefault();
s_logger.Info(string.Format("PrintPoolSize->openConnections={0} / maxPoolSize={1}", poolNumConnectionsCount, maxPoolSize));
}
catch (Exception ex)
{
s_logger.Error("PrintPoolSize: got exc; ", ex);
}
}
我看到我已经达到100/100连接。
我接下来要做的是在当前时间点打印所有打开的连接(SQL查询?),这可能吗?如果不是这样,我应该如何进行诊断?
我的系统需要几天/几周才能达到最大池大小,而不是立即达到。
我正在使用MySql.Data.MySQLClient
和MaxPoolSize
为100的情况。