The rows of a database query are never closed, which holds on to the connection
What it finds
rows, err := db.Query(q) in a function that never calls rows.Close(): the rows keep their connection busy until every row has been read, so a loop that stops early, or an error on the way, leaves the connection taken, and the pool runs dry.
The file must import database/sql, and the call must be a method Query or QueryContext whose two results go to a local variable and one more target. The function must not call Close on the variable, and the variable must not leave the function: rows that are returned, stored or passed to another function may be closed elsewhere.
From the rule's source, rows_not_closed.rs.
package p import "database/sql" func names(db *sql.DB) ([]string, error) { rows, err := db.Query("SELECT name FROM users")tx0 reports: the rows rows from Query are never closed if err != nil { return nil, err } var out []string for rows.Next() { var name string if err := rows.Scan(&name); err != nil { return nil, err } out = append(out, name) } return out, rows.Err()} func count(db *sql.DB) (int, error) { rows, err := db.Query("SELECT 1") if err != nil { return 0, err } defer rows.Close() n := 0 for rows.Next() { n++ } return n, rows.Err()} func query(db *sql.DB) (*sql.Rows, error) { rows, err := db.Query("SELECT 1") return rows, err} Turn it on, off or suppress it
The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:
[rules]
"tx0/go:rows-not-closed" = "warning"To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:
// tx0: ignore[tx0/go:rows-not-closed] reason="..."tx0 explain tx0/go:rows-not-closed shows the same in the terminal. Found a false positive? Report it.