“从游泳池获得连接时超时。” Hangfire.Postgres

时间:2015-01-28 21:20:44

标签: c# asp.net-mvc postgresql timeout hangfire

我是Hangfire的新手,所以我可能会搞砸到某个地方。我将Hangfire配置为:https://github.com/HangfireIO/Hangfire#installation

但不是:

 config.UseSqlServerStorage("<connection string or its name>");

我有:

 config.UsePostgreSqlStorage("Server=127.0.0.1;Port=5432;User Id=postgres;Password=pwd;Database=Hangfire");

所以我在我的数据库中创建了一个Hangfire数据库。

然后,我建立并运行我的项目。没关系。在我的postgres中创建Hangfire DB中的所有表。它运作得很好。

但是,当我尝试时:

   BackgroundJob.Enqueue(() => HubService.SendPushNotificationToUsers(threadParticipants,  messageApi.SenderId, messageApi.SenderName, messageApi.ThreadId, messageApi.Content));

我收到了InnerMessage的异常:

  "Timeout while getting a connection from pool." postgres

我错过了什么吗?

2 个答案:

答案 0 :(得分:1)

尝试通过ConnectionString或String Builder关闭连接池。

以下是我们的工作方式

        var sb = new NpgsqlConnectionStringBuilder(connectionString);
        sb.Pooling = false;
        app.UseHangfire(config =>
        {
            config.UseActivator(new WindsorJobActivator(container.Kernel));
            config.UsePostgreSqlStorage(sb.ToString(), new PostgreSqlStorageOptions() { UseNativeDatabaseTransactions = true });
            config.UseServer();
        });

答案 1 :(得分:1)

Did you try to reduce the amount of HangFire workers instead? Maybe these are consuming your connection pool as Hangfire uses by default 20 workers * X connections each (don't remember how many but they are several) and that could be consuming your connection pool... therefore the connection timeout...

You can set how many workers you want to use in the hangfire initialization. In this example you would use only 1 worker...

app.UseHangfire(config =>
{
    config.UseServer(1);
});