mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
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
This commit is contained in:
parent
59583b09f3
commit
0d12e24ebb
2 changed files with 25 additions and 2 deletions
|
|
@ -620,8 +620,8 @@ func (db *DB) conn() (*driverConn, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// If db.maxOpen > 0 and the number of open connections is over the limit
|
// 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.
|
// and there are no free connection, make a request and wait.
|
||||||
if db.maxOpen > 0 && (db.numOpen >= db.maxOpen || db.freeConn.Len() == 0) {
|
if db.maxOpen > 0 && db.numOpen >= db.maxOpen && db.freeConn.Len() == 0 {
|
||||||
// Make the connRequest channel. It's buffered so that the
|
// Make the connRequest channel. It's buffered so that the
|
||||||
// connectionOpener doesn't block while waiting for the req to be read.
|
// connectionOpener doesn't block while waiting for the req to be read.
|
||||||
ch := make(chan interface{}, 1)
|
ch := make(chan interface{}, 1)
|
||||||
|
|
|
||||||
|
|
@ -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
|
// golang.org/issue/5323
|
||||||
func TestStmtCloseDeps(t *testing.T) {
|
func TestStmtCloseDeps(t *testing.T) {
|
||||||
if testing.Short() {
|
if testing.Short() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue