Skip to content

Commit e5c285b

Browse files
committed
Discussion #250.
1 parent 6290a14 commit e5c285b

File tree

4 files changed

+13
-16
lines changed

4 files changed

+13
-16
lines changed

conn.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,9 @@ func (c *Conn) GetInterrupt() context.Context {
343343
//
344344
// https://sqlite.org/c3ref/interrupt.html
345345
func (c *Conn) SetInterrupt(ctx context.Context) (old context.Context) {
346+
if ctx == nil {
347+
panic("nil Context")
348+
}
346349
old = c.interrupt
347350
c.interrupt = ctx
348351
return old
@@ -406,11 +409,8 @@ func (c *Conn) BusyHandler(cb func(ctx context.Context, count int) (retry bool))
406409

407410
func busyCallback(ctx context.Context, mod api.Module, pDB ptr_t, count int32) (retry int32) {
408411
if c, ok := ctx.Value(connKey{}).(*Conn); ok && c.handle == pDB && c.busy != nil {
409-
interrupt := c.interrupt
410-
if interrupt == nil {
411-
interrupt = context.Background()
412-
}
413-
if interrupt.Err() == nil && c.busy(interrupt, int(count)) {
412+
if interrupt := c.interrupt; interrupt.Err() == nil &&
413+
c.busy(interrupt, int(count)) {
414414
retry = 1
415415
}
416416
}

driver/driver.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,10 @@ func (c *conn) Commit() error {
358358
}
359359

360360
func (c *conn) Rollback() error {
361-
err := c.Conn.Exec(`ROLLBACK` + c.txReset)
362-
if errors.Is(err, sqlite3.INTERRUPT) {
363-
old := c.Conn.SetInterrupt(context.Background())
364-
defer c.Conn.SetInterrupt(old)
365-
err = c.Conn.Exec(`ROLLBACK` + c.txReset)
366-
}
367-
return err
361+
// ROLLBACK even if interrupted.
362+
old := c.Conn.SetInterrupt(context.Background())
363+
defer c.Conn.SetInterrupt(old)
364+
return c.Conn.Exec(`ROLLBACK` + c.txReset)
368365
}
369366

370367
func (c *conn) Prepare(query string) (driver.Stmt, error) {

tests/conn_test.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,7 @@ func TestConn_SetInterrupt(t *testing.T) {
146146
}
147147
defer stmt.Close()
148148

149-
go func() {
150-
time.Sleep(time.Millisecond)
151-
cancel()
152-
}()
149+
time.AfterFunc(time.Millisecond, cancel)
153150

154151
// Interrupting works.
155152
err = stmt.Exec()

txn.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type Txn struct {
2020
}
2121

2222
// Begin starts a deferred transaction.
23+
// Panics if a transaction is already in-progress.
24+
// For nested transactions, use [Conn.Savepoint].
2325
//
2426
// https://sqlite.org/lang_transaction.html
2527
func (c *Conn) Begin() Txn {
@@ -119,6 +121,7 @@ func (tx Txn) Commit() error {
119121
//
120122
// https://sqlite.org/lang_transaction.html
121123
func (tx Txn) Rollback() error {
124+
// ROLLBACK even if interrupted.
122125
return tx.c.exec(`ROLLBACK`)
123126
}
124127

0 commit comments

Comments
 (0)