环境:net core 3.1
Microsoft.Data.SqlClient和System.Data.SqlClient都报这个错误
接口如下
public async Task GetOrgan2List()
{
using (var con = new SqlConnection(GlobalConfig.ConnectionString))
{
con.Open();
await Task.Delay(300);
con.Close();
con.Dispose();
}
}
压测10秒,999并发
ab -t 10 -c 999
报错
System.InvalidOperationException: 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.
at Microsoft.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
at Microsoft.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
at Microsoft.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
at Microsoft.Data.SqlClient.SqlConnection.Open()
at SP.Api.Areas.Exam.v1.Controllers.OrganController.GetOrgan2List()
连接池爆了吧,打开太多tcp连接了。你压这个的目的是啥?
using (var con = new SqlConnection(GlobalConfig.ConnectionString))
{
con.Open();
await Task.Delay(300);
}
这样就可以, 连接池 需要通过字符串 配置(Max Pool Size 是连接池最大连接数,Min Pool Size 最少连接数)
Server=(local); Integrated Security=SSPI; Database=Northwind; Max Pool Size=512; Min Pool Size=5