Commit 3cdf8bae authored by Brad Fitzpatrick's avatar Brad Fitzpatrick

database/sql: fix Conn leak

Fixes #4902

R=golang-dev, alex.brainman, r, google
CC=golang-dev
https://golang.org/cl/7579045
parent 1a4599b4
......@@ -445,6 +445,7 @@ func (db *DB) prepare(query string) (*Stmt, error) {
css: []connStmt{{ci, si}},
}
db.addDep(stmt, stmt)
db.putConn(ci, nil)
return stmt, nil
}
......
......@@ -708,3 +708,32 @@ func TestQueryRowNilScanDest(t *testing.T) {
t.Errorf("error = %q; want %q", err.Error(), want)
}
}
func TestIssue4902(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
driver := db.driver.(*fakeDriver)
opens0 := driver.openCount
var stmt *Stmt
var err error
for i := 0; i < 10; i++ {
stmt, err = db.Prepare("SELECT|people|name|")
if err != nil {
t.Fatal(err)
}
err = stmt.Close()
if err != nil {
t.Fatal(err)
}
}
opens := driver.openCount - opens0
if opens > 1 {
t.Errorf("opens = %d; want <= 1", opens)
t.Logf("db = %#v", db)
t.Logf("driver = %#v", driver)
t.Logf("stmt = %#v", stmt)
}
}
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