diff options
| author | Gwenael Treguier <gwenn.kahz@gmail.com> | 2012-03-10 15:21:44 -0800 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2012-03-10 15:21:44 -0800 |
| commit | c3954dd5da820306dff6bbd73a7801d9739b038f (patch) | |
| tree | f04ce21d78b1599a90da8993f59d9db455109237 /src/pkg/database/sql/sql.go | |
| parent | 959d0c7ac0091513fc1a2e53834d027430122ee8 (diff) | |
| download | go-c3954dd5da820306dff6bbd73a7801d9739b038f.tar.xz | |
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
Diffstat (limited to 'src/pkg/database/sql/sql.go')
| -rw-r--r-- | src/pkg/database/sql/sql.go | 8 |
1 files changed, 5 insertions, 3 deletions
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 |
