Commit 73fe6123 authored by Ian Gudger's avatar Ian Gudger Committed by Brad Fitzpatrick

database/sql: fix case where Stmt.Close discards error

Fixes a case where the Stmt.Close() function in database/sql discards any error generated by the Close() function of the contained driverStmt.

Fixes #12798

Change-Id: I40384d6165856665b062d15a643e4ecc09d63fda
Reviewed-on: https://go-review.googlesource.com/15178Reviewed-by: default avatarBrad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
parent f80ff56a
......@@ -1576,9 +1576,9 @@ func (s *Stmt) Close() error {
s.closed = true
if s.tx != nil {
s.txsi.Close()
err := s.txsi.Close()
s.mu.Unlock()
return nil
return err
}
s.mu.Unlock()
......
......@@ -356,6 +356,44 @@ func TestStatementQueryRow(t *testing.T) {
}
}
type stubDriverStmt struct {
err error
}
func (s stubDriverStmt) Close() error {
return s.err
}
func (s stubDriverStmt) NumInput() int {
return -1
}
func (s stubDriverStmt) Exec(args []driver.Value) (driver.Result, error) {
return nil, nil
}
func (s stubDriverStmt) Query(args []driver.Value) (driver.Rows, error) {
return nil, nil
}
// golang.org/issue/12798
func TestStatementClose(t *testing.T) {
want := errors.New("STMT ERROR")
tests := []struct {
stmt *Stmt
msg string
}{
{&Stmt{stickyErr: want}, "stickyErr not propagated"},
{&Stmt{tx: &Tx{}, txsi: &driverStmt{&sync.Mutex{}, stubDriverStmt{want}}}, "driverStmt.Close() error not propagated"},
}
for _, test := range tests {
if err := test.stmt.Close(); err != want {
t.Errorf("%s. Got stmt.Close() = %v, want = %v", test.msg, err, want)
}
}
}
// golang.org/issue/3734
func TestStatementQueryRowConcurrent(t *testing.T) {
db := newTestDB(t, "people")
......
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