go resource_error ai_generated true

sql:连接池耗尽:打开连接过多(max_open_conns=10)

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-02-18首次发现

版本兼容性

版本状态引入弃用备注
1.18 active — — —
1.21 active — — —

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 80% 失败

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

  2. 90% 失败

    Does not release leaked connections; just delays the failure.