diff options
author | Russ Cox <rsc@golang.org> | 2014-02-27 22:43:10 -0500 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2014-02-27 22:43:10 -0500 |
commit | 17710a23c450bba77c8eaddc19e88bb428fff391 (patch) | |
tree | 89a60f2070ad847ec6cca9d1ed18555caf0ef539 | |
parent | 5439d23b19986397288e70c281d48d09acbac7b3 (diff) | |
download | go-17710a23c450bba77c8eaddc19e88bb428fff391.tar.gz |
[release-branch.go1.2] database/sql: Use all connections in pool
??? CL 40410043 / 8a7ac002f840
database/sql: Use all connections in pool
The last connection in the pool was not being handed out correctly.
R=golang-codereviews, gobot, bradfitz
CC=golang-codereviews
https://codereview.appspot.com/40410043
Committer: Brad Fitzpatrick <bradfitz@golang.org>
???
LGTM=r
R=golang-codereviews, r
CC=golang-dev
https://codereview.appspot.com/68820044
-rw-r--r-- | src/pkg/database/sql/sql.go | 4 | ||||
-rw-r--r-- | src/pkg/database/sql/sql_test.go | 23 |
2 files changed, 25 insertions, 2 deletions
diff --git a/src/pkg/database/sql/sql.go b/src/pkg/database/sql/sql.go index dddf5a3f2..84a096513 100644 --- a/src/pkg/database/sql/sql.go +++ b/src/pkg/database/sql/sql.go @@ -620,8 +620,8 @@ func (db *DB) conn() (*driverConn, error) { } // If db.maxOpen > 0 and the number of open connections is over the limit - // or there are no free connection, then make a request and wait. - if db.maxOpen > 0 && (db.numOpen >= db.maxOpen || db.freeConn.Len() == 0) { + // and there are no free connection, make a request and wait. + if db.maxOpen > 0 && db.numOpen >= db.maxOpen && db.freeConn.Len() == 0 { // Make the connRequest channel. It's buffered so that the // connectionOpener doesn't block while waiting for the req to be read. ch := make(chan interface{}, 1) diff --git a/src/pkg/database/sql/sql_test.go b/src/pkg/database/sql/sql_test.go index 093c0d64c..787a5c9f7 100644 --- a/src/pkg/database/sql/sql_test.go +++ b/src/pkg/database/sql/sql_test.go @@ -1005,6 +1005,29 @@ func TestMaxOpenConns(t *testing.T) { } } +func TestSingleOpenConn(t *testing.T) { + db := newTestDB(t, "people") + defer closeDB(t, db) + + db.SetMaxOpenConns(1) + + rows, err := db.Query("SELECT|people|name|") + if err != nil { + t.Fatal(err) + } + if err = rows.Close(); err != nil { + t.Fatal(err) + } + // shouldn't deadlock + rows, err = db.Query("SELECT|people|name|") + if err != nil { + t.Fatal(err) + } + if err = rows.Close(); err != nil { + t.Fatal(err) + } +} + // golang.org/issue/5323 func TestStmtCloseDeps(t *testing.T) { if testing.Short() { |