Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

database/sql: DB.Conn always returns a cached connection #29684

Closed
boekkooi-fresh opened this issue Jan 11, 2019 · 4 comments
Closed

database/sql: DB.Conn always returns a cached connection #29684

boekkooi-fresh opened this issue Jan 11, 2019 · 4 comments

Comments

@boekkooi-fresh
Copy link

@boekkooi-fresh boekkooi-fresh commented Jan 11, 2019

What version of Go are you using (go version)?

$ go version
go version go1.11.3 linux/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN="/data/go/bin"
GOCACHE="/home/<>/.cache/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="linux"
GOOS="linux"
GOPATH="/data/go"
GOPROXY=""
GORACE=""
GOROOT="/home/<>/.goenv/versions/1.11.3"
GOTMPDIR=""
GOTOOLDIR="/home/<>/.goenv/versions/1.11.3/pkg/tool/linux_amd64"
GCCGO="gccgo"
CC="gcc"
CXX="g++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fmessage-length=0 -fdebug-prefix-map=/tmp/go-build956395453=/tmp/go-build -gno-record-gcc-switches"

What did you do?

When using sql.DB.Conn() a bad sql.ErrBadConn error was returned because the connections were terminated by the server (using pg_terminate_backend on postgres).
This by it self is not a bad thing but seems to not confirm with sql.DB.ExecContext() and sql.DB.PingContext() which actually kept working by opening a new connection.

While looking at the code I noticed that sq.DB.ExecContext() did cachedOrNewConn and then alwaysNewConn to ensure a new connection was available.
The code in sql.DB.Conn() seems to be doing a cachedOrNewConn and then cachedOrNewConn causing a cached/bad connection to be returned.

Probably this can be fixed by replacing src/database/sql/sql.go line 1701:

dc, err = db.conn(ctx, cachedOrNewConnalwaysNewConn)" 

What did you expect to see?

I expected sql.DB.Conn() to use the cached connections and otherwise create a new one similar to the rest of the library.

What did you see instead?

sql.DB.Conn() returned a cached/bad connection and did not try to create a new one.

@julieqiu julieqiu changed the title sql.DB.Conn always returns a cached connection database/sql: DB.Conn always returns a cached connection Jan 11, 2019
@julieqiu
Copy link
Contributor

@julieqiu julieqiu commented Jan 11, 2019

@kardianos
Copy link
Contributor

@kardianos kardianos commented Jan 11, 2019

That might have been a copy and paste error. I think you're right about this.

@gopherbot
Copy link

@gopherbot gopherbot commented Jan 11, 2019

Change https://golang.org/cl/157637 mentions this issue: database/sql: fix logic for pulling a Conn from DB

@bradfitz
Copy link
Contributor

@bradfitz bradfitz commented Jan 14, 2019

@gopherbot, please cherry-pick to Go 1.11.

(Daniel, I assume this was a Go 1.11 regression from Go 1.10?)

@bradfitz bradfitz added this to the Go1.12 milestone Jan 14, 2019
@gopherbot gopherbot closed this in 70931c0 Jan 14, 2019
@golang golang locked and limited conversation to collaborators Jan 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.