go resource_error ai_generated true

sql: connection pool exhausted: too many open connections (max_open_conns=10)

ID: go/sql-too-many-open-connections

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-02-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.18 active — — —
1.21 active — — —

Root Cause

Handlers acquire connections but never Close rows or return them, or MaxOpenConns is set too low for concurrency. Leaked *sql.Rows exhaust the pool.

generic

中文

处理程序获取连接后未 Close rows 或归还连接,或 MaxOpenConns 对并发量设置过低。未关闭的 *sql.Rows 会耗尽连接池。

Workarounds

  1. 92% success
    rows, err := db.QueryContext(ctx, q)
    if err != nil { return err }
    defer rows.Close()
    for rows.Next() { ... }
    return rows.Err()
  2. 85% success
    db.SetMaxOpenConns(50)
    db.SetMaxIdleConns(25)
    db.SetConnMaxLifetime(5*time.Minute)

Dead Ends

Common approaches that don't work:

  1. 80% fail

    Hides the leak and can exhaust the database server's connection limit.

  2. 90% fail

    Does not release leaked connections; just delays the failure.