From 727ad200a2f6cdd698bb0e3253f51d36d269bb9e Mon Sep 17 00:00:00 2001 From: sqweek Date: Mon, 18 Apr 2016 19:49:17 +0800 Subject: [PATCH] Fix inconsistent tx state with database/sql. The semantics of sql.Tx.Commit impose that the transaction is finished and cleaned up by the time the driver's Commit function returns. However sqlite3 leaves the transaction open if COMMIT fails due to an SQLITE_BUSY error, so *we* must clean it up. Closes #184. --- sqlite3.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sqlite3.go b/sqlite3.go index 4d81917..86f686c 100644 --- a/sqlite3.go +++ b/sqlite3.go @@ -292,6 +292,12 @@ func (ai *aggInfo) Done(ctx *C.sqlite3_context) { // Commit transaction. func (tx *SQLiteTx) Commit() error { _, err := tx.c.exec("COMMIT") + if err != nil && err.(Error).Code == C.SQLITE_BUSY { + // sqlite3 will 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. + tx.c.exec("ROLLBACK") + } return err }