From c3954dd5da820306dff6bbd73a7801d9739b038f Mon Sep 17 00:00:00 2001 From: Gwenael Treguier Date: Sat, 10 Mar 2012 15:21:44 -0800 Subject: database/sql: ensure Stmts are correctly closed. To make sure that there is no resource leak, I suggest to fix the 'fakedb' driver such as it fails when any Stmt is not closed. First, add a check in fakeConn.Close(). Then, fix all missing Stmt.Close()/Rows.Close(). I am not sure that the strategy choose in fakeConn.Prepare/prepare* is ok. The weak point in this patch is the change in Tx.Query: - Tests pass without this change, - I found it by manually analyzing the code, - I just try to make Tx.Query look like DB.Query. R=golang-dev, bradfitz CC=golang-dev https://golang.org/cl/5759050 --- src/pkg/database/sql/sql.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/pkg/database/sql/sql.go') diff --git a/src/pkg/database/sql/sql.go b/src/pkg/database/sql/sql.go index c00425d8fa..51a357b37d 100644 --- a/src/pkg/database/sql/sql.go +++ b/src/pkg/database/sql/sql.go @@ -612,9 +612,11 @@ func (tx *Tx) Query(query string, args ...interface{}) (*Rows, error) { return nil, err } rows, err := stmt.Query(args...) - if err == nil { - rows.closeStmt = stmt + if err != nil { + stmt.Close() + return nil, err } + rows.closeStmt = stmt return rows, err } @@ -1020,7 +1022,7 @@ func (r *Row) Scan(dest ...interface{}) error { } // TODO(bradfitz): for now we need to defensively clone all - // []byte that the driver returned (not permitting + // []byte that the driver returned (not permitting // *RawBytes in Rows.Scan), since we're about to close // the Rows in our defer, when we return from this function. // the contract with the driver.Next(...) interface is that it -- cgit v1.3