]> Cypherpunks repositories - gostls13.git/commitdiff
database/sql: Use all connections in pool
authorMarko Tiikkaja <marko@joh.to>
Thu, 26 Dec 2013 19:27:18 +0000 (11:27 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 26 Dec 2013 19:27:18 +0000 (11:27 -0800)
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

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

index 41527c8db199bc2dbf64b9968fc1cb7194e9e055..a0bd05162813d41716161744162e44a616b46735 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 aac36f87d3bc945dc8bff381928dc506ed41e5f2..a0a20df6f83600d757ee4e6e0ad37072907fc2a9 100644 (file)
@@ -1033,6 +1033,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() {