服务器空闲几个小时后,sqlalchemy 无法连接到数据库

发布于 2024-11-18 19:04:07 字数 347 浏览 0 评论 0原文

我有一个相当标准的设置,其中应用程序服务器通过防火墙与数据库服务器分开。我们使用 sqlalchemy 的应用程序和 db 是 postgres。

系统可以正常工作几个小时,但是一旦长时间处于空闲状态,服务器和数据库之间的通信就会失败。错误日志是:

WARNI [sqlalchemy.pool.QueuePool.0x...7310] [worker 26] Error closing cursor: cursor already closed

我将连接池的“pool_recylce”设置设置为 30 分钟,这样就没有连接持续超过 30 分钟。问题仍然存在。

有什么想法吗?

I have a fairly standard setup where the application server is separated by a firewall from the database server. The application us using sqlalchemy and the db is postgres.

The system works fine for a few hours, but once it remains idle for a long period it appears that the communication fails between the server and the db. The error log is:

WARNI [sqlalchemy.pool.QueuePool.0x...7310] [worker 26] Error closing cursor: cursor already closed

I set up the connection pool with the 'pool_recylce' setting set to 30mins, so that there are no connections lingering for more than 30 mins. The problem persists.

any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧夏天 2024-11-25 19:04:07

您可能在客户端和服务器之间有一个状态防火墙,它会忘记闲置时间过长的连接。

您需要:

  • 配置您的防火墙以更长时间地记住连接 - 但这可能是不可能的;
  • 配置 keepalive服务器,例如这样:
    tcp_keepalives_idle=600
    tcp_keepalives_interval=30
    tcp_keepalives_count = 60
    
    这意味着如果连接空闲 10 分钟,尝试每 30 秒发送一次 keepalive 探测,直到响应,如果忽略 60 个探测,则关闭连接(连接中断 30 分钟);
    
    
  • 使用 `keepalives_idle`、`keepalives_interval` 和 `keepalives_count` 在客户端中配置 keepalive 连接参数;我不知道在 SQLAlchemy 中是否可以;

禁用pool_recycle,因为恕我直言,它仅在关闭连接时才有效。

You probably have a stateful firewall between a client and a server, which is forgetting about connections that are idle for too long.

You need either to:

  • configure your firewall to remember connections longer — it may not be possible though;
  • configure keepalive on the server, for example like this:
    tcp_keepalives_idle=600
    tcp_keepalives_interval=30
    tcp_keepalives_count=60
    

    Which means that if connection is idle for 10 minutes try to send keepalive probes every 30 seconds until response, close connection if 60 probes are ignored (connection is broken for 30 minutes);

  • configure keepalive in client using `keepalives_idle`, `keepalives_interval` and `keepalives_count` connection parameters; I don't know if it is possible in SQLAlchemy;

Disable pool_recycle as IMHO it works only if you close a connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文