Commit ae9712c7 authored by Daniel Theophanes's avatar Daniel Theophanes Committed by Brad Fitzpatrick

database/sql: ensure Commit and Rollback return ErrTxDone

Ensure documented behavior of returning ErrTxDone if the Tx has
already been committed or rolled back.

Fixes #18147

Change-Id: I07dc75bef4dbd4dd88dd252c96dc8ab99f28c00e
Reviewed-on: https://go-review.googlesource.com/33793
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
parent 2d136ae8
......@@ -1449,14 +1449,14 @@ func (tx *Tx) closePrepared() {
// Commit commits the transaction.
func (tx *Tx) Commit() error {
if tx.isDone() {
return ErrTxDone
}
select {
default:
case <-tx.ctx.Done():
return tx.ctx.Err()
}
if tx.isDone() {
return ErrTxDone
}
var err error
withLock(tx.dc, func() {
err = tx.txi.Commit()
......
......@@ -683,6 +683,37 @@ func TestQueryRow(t *testing.T) {
}
}
func TestTxRollbackCommitErr(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
tx, err := db.Begin()
if err != nil {
t.Fatal(err)
}
err = tx.Rollback()
if err != nil {
t.Errorf("expected nil error from Rollback; got %v", err)
}
err = tx.Commit()
if err != ErrTxDone {
t.Errorf("expected %q from Commit; got %q", ErrTxDone, err)
}
tx, err = db.Begin()
if err != nil {
t.Fatal(err)
}
err = tx.Commit()
if err != nil {
t.Errorf("expected nil error from Commit; got %v", err)
}
err = tx.Rollback()
if err != ErrTxDone {
t.Errorf("expected %q from Rollback; got %q", ErrTxDone, err)
}
}
func TestStatementErrorAfterClose(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment