From d737639c4acce7c9f28de22eabfd623d0ae7ed50 Mon Sep 17 00:00:00 2001 From: Chris Hines Date: Mon, 24 Aug 2015 21:48:39 -0400 Subject: 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/database/sql/sql.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/database/sql/sql.go') diff --git a/src/database/sql/sql.go b/src/database/sql/sql.go index aaa4ea28be..0120ae8abe 100644 --- a/src/database/sql/sql.go +++ b/src/database/sql/sql.go @@ -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 } -- cgit v1.3