go resource_error ai_generated true

sql: connection pool exhausted: too many connections

ID: go/sql-connection-pool-exhausted

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.20 active

Root Cause

More concurrent database requests than available connections; pool max limit reached.

generic

中文

并发数据库请求超过可用连接数,达到连接池最大限制。

Workarounds

  1. 90% success
    Use connection pooling with SetMaxOpenConns and SetMaxIdleConns, and use context timeouts: db.SetMaxOpenConns(10); db.SetConnMaxLifetime(time.Minute)
  2. 85% success
    Implement a semaphore to limit concurrent queries: sem := make(chan struct{}, 10); sem <- struct{}{}; defer func(){<-sem}()

Dead Ends

Common approaches that don't work:

  1. 60% fail

    Unlimited connections can overwhelm database server.

  2. 50% fail

    May not be feasible; pool still limited.