go config_error ai_generated true

error: sql: connection returned from pool was closed by the server

ID: go/database-sql-conn-max-lifetime-exceeded

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-06-15First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active
1.21 active

Root Cause

The database server closed the connection due to idle timeout, but the connection pool returned it as still valid.

generic

中文

数据库服务器因空闲超时关闭了连接,但连接池仍将其视为有效并返回。

Workarounds

  1. 90% success Set ConnMaxLifetime to less than server's wait_timeout
    db.SetConnMaxLifetime(5 * time.Minute)
    db.SetMaxIdleConns(10)

Dead Ends

Common approaches that don't work:

  1. Setting ConnMaxLifetime too high 60% fail

    Long-lived connections may be closed by server anyway; need to match server timeout.