sql server连接池爆满排查解决定位
Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached.
目前发现 部署的webapi无法登录了,但是我这边自己本地跑发现可以登录,临时解决办法是webapi那边重启。
但是由于是本地代码 无法反射定位内存泄露。。
这个方法是行不通了
using System;
using System.Data.SqlClient;
using System.Reflection;
class Program
{
static void Main()
{
var conn = new SqlConnection("YourConnectionStringHere");
conn.Open();
// 通过反射获取 SqlConnection 内部的连接池状态
var type = typeof(SqlConnection);
var connectionFactoryField = type.GetField("s_connectionFactory", BindingFlags.NonPublic | BindingFlags.Static);
if (connectionFactoryField != null)
{
var connectionFactory = connectionFactoryField.GetValue(null);
var poolGroupField = connectionFactory.GetType().GetField("_poolGroupList", BindingFlags.NonPublic | BindingFlags.Instance);
var poolGroup = poolGroupField.GetValue(connectionFactory);
Console.WriteLine(poolGroup?.ToString());
}
conn.Close();
}
}
改大连接池
"YourConnectionStringHere;Max Pool Size=200;"
最后还有一个死办法
[HttpGet("relese")]
public string release(string pwd)
{
if (pwd == "aaaaaaaaa") {
SqlConnection.ClearAllPools();
return "OK";
}
else {
return "无法释放 密码错误";
}
}
但是这些方法都不行,下面的方法彻底定位解决