Skip to content

Commit

Permalink
Fix inconsistent tx state with database/sql.
Browse files Browse the repository at this point in the history
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 mattn#184.
  • Loading branch information
sqweek committed Apr 18, 2016
1 parent 22d7351 commit 727ad20
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 727ad20

Please sign in to comment.