python network_error ai_generated true

sqlalchemy.exc.OperationalError:(psycopg2.OperationalError) SSL 连接意外关闭

sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) SSL connection has been closed unexpectedly

ID: python/sqlalchemy-connection-pool-recycle

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2025-09-01首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

数据库服务器因超时关闭了空闲连接;连接池仍持有过时连接。

English

Database server closed idle connections due to timeout; pool still holds stale connections.

generic

解决方案

  1. 95% 成功率 Set pool_recycle to a value less than server idle timeout.
    engine = create_engine(url, pool_recycle=3600)  # Recycle connections every hour
  2. 90% 成功率 Enable pool_pre_ping to check connection health.
    engine = create_engine(url, pool_pre_ping=True)

无效尝试

常见但无效的做法:

  1. Disabling SSL entirely. 50% 失败

    Security risk; may not be allowed by policy.

  2. Increasing pool_timeout. 70% 失败

    Does not refresh stale connections.