From ce6b75dab634e272e0449f85853fca7f1850da8b Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Wed, 7 May 2014 11:54:29 -0700 Subject: [PATCH] database/sql: fix accounting of open connections Existing test TestMaxOpenConns was failing occasionally, especially with higher values of GOMAXPROCS. Fixes #7532 LGTM=iant R=golang-codereviews, iant CC=golang-codereviews https://golang.org/cl/95130043 --- src/pkg/database/sql/sql.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/pkg/database/sql/sql.go b/src/pkg/database/sql/sql.go index 4874574c30..ef54dcdf91 100644 --- a/src/pkg/database/sql/sql.go +++ b/src/pkg/database/sql/sql.go @@ -652,13 +652,16 @@ func (db *DB) conn() (*driverConn, error) { return conn, nil } + db.numOpen++ // optimistically db.mu.Unlock() ci, err := db.driver.Open(db.dsn) if err != nil { + db.mu.Lock() + db.numOpen-- // correct for earlier optimism + db.mu.Unlock() return nil, err } db.mu.Lock() - db.numOpen++ dc := &driverConn{ db: db, ci: ci,