python network_error ai_generated true

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

ID: python/sqlalchemy-connection-pool-recycle

Also available as: JSON · Markdown · 中文
80%Fix Rate
85%Confidence
0Evidence
2025-09-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

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

generic

中文

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

Workarounds

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

Dead Ends

Common approaches that don't work:

  1. Disabling SSL entirely. 50% fail

    Security risk; may not be allowed by policy.

  2. Increasing pool_timeout. 70% fail

    Does not refresh stale connections.