Skip to content

Commit

Permalink
Always rollback on a commit error
Browse files Browse the repository at this point in the history
  • Loading branch information
joshbuddy committed Aug 28, 2022
1 parent 7b40448 commit d07c0fc
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions sqlite3.go
Expand Up @@ -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
Expand Down

0 comments on commit d07c0fc

Please sign in to comment.