# error: sql: database is closed

- **ID:** `go/database-sql-connection-bad-connection`
- **Domain:** go
- **Category:** network_error
- **Verification:** ai_generated
- **Fix Rate:** 80%

## Root Cause

Attempting to use a database connection after the database has been closed, typically due to calling db.Close() prematurely.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| 1.20 | active | — | — |
| 1.21 | active | — | — |

## Workarounds

1. **Use a single db instance and close only when shutting down** (95% success)
   ```
   db, err := sql.Open("driver", dsn)
if err != nil { log.Fatal(err) }
defer db.Close()
// use db for all queries
   ```

## Dead Ends

- **Reopening database for each query** — Inefficient; use connection pool and defer close at application shutdown. (70% fail)
