Go `database/sql` Connection Pool Exhausted (Unclosed `*sql.Rows`)

Returning early without `defer rows.Close()` left DB connections checked out indefinitely.

Runs locally in browser Free · No login Independently Verified 2026-11-25 v4.1.0
Identifier: Go `database/sql` Connection Pool Exhausted (Unclosed `*sql.Rows`) Platform: Go
Common Root Causes:
  • Returning early without `defer rows.Close()` left DB connections checked out indefinitely.

Interactive Diagnostic Checklist

Verified Solutions:
  1. Always write `defer rows.Close()` immediately after checking `err != nil` and check `rows.Err()`.
Safe Diagnostic Command:
Check `db.Stats()` open/in-use counts
Warning: Verify configuration changes in staging or with dry-run flags (e.g. nginx -t) before reloading production services.

Technical Explanation

Returning early without `defer rows.Close()` left DB connections checked out indefinitely. Recommended diagnostic: `Check `db.Stats()` open/in-use counts`. Remediation: Always write `defer rows.Close()` immediately after checking `err != nil` and check `rows.Err()`.

Worked Examples

  • Run `Check `db.Stats()` open/in-use counts` to verify root cause before applying the remediation.

Limitations & Assumptions

  • Always verify configuration changes in staging or with dry-run flags (e.g. `nginx -t`) before reloading production services.

Source References & Verification

Resource ID: err-go-sql-rows-close-connection-leak · Independently verified: 2026-11-25

Related Resources