A database transaction is started in a function that never rolls it back
What it finds
tx, err := db.Begin() in a function that calls tx.Commit() but never tx.Rollback(): every return between the two, usually on an error, leaves the transaction open, holding its connection and its locks until the database gives up on it. The usual pattern is defer tx.Rollback() right after the error check, which does nothing once Commit has succeeded.
The file must import database/sql, and the call must be a method Begin or BeginTx whose two results go to a local variable and one more target. The function must not call Rollback on the variable, and the variable must not leave the function.
From the rule's source, transaction_not_rolled_back.rs.
package p import "database/sql" func rename(db *sql.DB, id int, name string) error { tx, err := db.Begin()tx0 reports: the transaction tx is never rolled back if err != nil { return err } if _, err := tx.Exec("UPDATE users SET name = ? WHERE id = ?", name, id); err != nil { return err } return tx.Commit()} func remove(db *sql.DB, id int) error { tx, err := db.Begin() if err != nil { return err } defer tx.Rollback() if _, err := tx.Exec("DELETE FROM users WHERE id = ?", id); err != nil { return err } return tx.Commit()} func begin(db *sql.DB) (*sql.Tx, error) { tx, err := db.Begin() return tx, 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:transaction-not-rolled-back" = "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:transaction-not-rolled-back] reason="..."tx0 explain tx0/go:transaction-not-rolled-back shows the same in the terminal. Found a false positive? Report it.