From d07c0fc3e74aac73ddf526cadae4bf37cd05e134 Mon Sep 17 00:00:00 2001 From: Joshua Hull Date: Mon, 22 Aug 2022 09:56:25 +0200 Subject: [PATCH] Always rollback on a commit error --- sqlite3.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sqlite3.go b/sqlite3.go index 6e67e7cc..9c0f4d89 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -494,10 +494,12 @@ func (ai *aggInfo) Done(ctx *C.sqlite3_context) { // Commit transaction. func (tx *SQLiteTx) Commit() error { _, err := tx.c.exec(context.Background(), "COMMIT", nil) - if err != nil && (err.(Error).Code == C.SQLITE_BUSY || err.(Error).Code == C.SQLITE_CONSTRAINT) { - // sqlite3 will leave the transaction open in this scenario. + if err != nil { + // sqlite3 may leave the transaction open in this scenario. // However, database/sql considers the transaction complete once we // return from Commit() - we must clean up to honour its semantics. + // We don't know if the ROLLBACK is strictly necessary, but according + // to sqlite's docs, there is no harm in calling ROLLBACK unnecessarily. tx.c.exec(context.Background(), "ROLLBACK", nil) } return err