]> Cypherpunks repositories - gostls13.git/commitdiff
[release-branch.go1.2] database/sql: Use all connections in pool
authorRuss Cox <rsc@golang.org>
Fri, 28 Feb 2014 03:43:10 +0000 (22:43 -0500)
committerBrad Fitzpatrick <bradfitz@golang.org>
Fri, 28 Feb 2014 03:43:10 +0000 (22:43 -0500)
««« 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://golang.org/cl/40410043

»»»

LGTM=r
R=golang-codereviews, r
CC=golang-dev
https://golang.org/cl/68820044

src/pkg/database/sql/sql.go
src/pkg/database/sql/sql_test.go

index dddf5a3f257257f78b7d95acbe108198ae8df200..84a096513221bc197aa148ea5de34cfede84b28b 100644 (file)
@@ -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)
index 093c0d64caceecc170153abf96d500ccf7372097..787a5c9f74423aeeda907390ee6979285a210f15 100644 (file)
@@ -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() {