最近在一个 ASP.NET Core 应用的日志中发现数据库连接池中的连接被用光的错误:
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 System.Data.Common.ADP.ExceptionWithStackTrace(Exception e)
而这个项目与其他项目主要不同之处是使用了 .NET Core 2.1 引入的 DbContextPool
services.AddDbContextPool<JobDb>(option =>
option.UseSqlServer(Configuration.DbConnectionStr(), builder =>
{
builder.EnableRetryOnFailure(
maxRetryCount: 5,
maxRetryDelay: TimeSpan.FromSeconds(10),
errorNumbersToAdd: new int[] { 2 });
}));
昨天去掉 DbContextPool 之后到目前未出现问题,疑似 EF Core 的 DbContextPool 会造成数据库连接泄漏,请问如何定位是否是 DbContextPool 的bug ?