database/sql: close bad connections in commit or rollback:

Previously Tx.close always passed a nil error to tx.db.putConn. As a
result bad connections were reused, even if the driver returned
driver.ErrBadConn. Adding an err parameter to Tx.close allows it to
receive the driver error from Tx.Commit and Tx.Rollback and pass it
to tx.db.putConn.

Fixes #11264

Change-Id: I142b6b2509fa8d714bbc135cef7281a40803b3b8
Reviewed-on: https://go-review.googlesource.com/13912
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Chris Hines 2015-08-24 21:48:39 -04:00 committed by Brad Fitzpatrick
parent bf99d8f843
commit d737639c4a
3 changed files with 87 additions and 4 deletions

View file

@ -1103,12 +1103,12 @@ type Tx struct {
var ErrTxDone = errors.New("sql: Transaction has already been committed or rolled back")
func (tx *Tx) close() {
func (tx *Tx) close(err error) {
if tx.done {
panic("double close") // internal error
}
tx.done = true
tx.db.putConn(tx.dc, nil)
tx.db.putConn(tx.dc, err)
tx.dc = nil
tx.txi = nil
}
@ -1134,13 +1134,13 @@ func (tx *Tx) Commit() error {
if tx.done {
return ErrTxDone
}
defer tx.close()
tx.dc.Lock()
err := tx.txi.Commit()
tx.dc.Unlock()
if err != driver.ErrBadConn {
tx.closePrepared()
}
tx.close(err)
return err
}
@ -1149,13 +1149,13 @@ func (tx *Tx) Rollback() error {
if tx.done {
return ErrTxDone
}
defer tx.close()
tx.dc.Lock()
err := tx.txi.Rollback()
tx.dc.Unlock()
if err != driver.ErrBadConn {
tx.closePrepared()
}
tx.close(err)
return err
}