Npgsql,连接池已经用尽

时间:2018-03-21 08:31:17

标签: c# .net postgresql entity-framework npgsql

我从这开始 - SemaphoreSlim to protect the connection pool from exhaustion,并很快意识到我的问题是当我处理DbContext时连接没有返回到池中。

考虑所有与数据库的连接都是在using语句中完成的,如下所示:

using (var context = ContextFactory.GetContext<PostgreSqlDatabaseContext>(connectionString))
{
    var query = $"SELECT * FROM public.\"MyTable\" WHERE \"MyId\" = '{id}'";
    var result = await context.MyTable.FromSql(query).SingleOrDefaultAsync();
    return result;
}

ContextFactory看起来像这样:

internal class ContextFactory
{
    public static T GetContext<T>(string sqlConnection) where T : DbContext
    {
        var optionsBuilder = new DbContextOptionsBuilder<PostgreSqlDatabaseContext>();
        optionsBuilder.UseNpgsql(sqlConnection);
        return new PostgreSqlDatabaseContext(optionsBuilder.Options) as T;
    }
}

无论我使用连接字符串做什么,按建议here设置Enlist=true,或按建议使用SemaphoreSlim限制连接here我得到相同的结果:< / p>

  

连接池已用完,要么提升MaxPoolSize   (目前为20)或超时(目前为15秒)

Npgsql是否有效?这是怎么回事?

请注意,我已阅读thisthis,但它们并不相同。

0 个答案:

没有答案
相关问题