go: database/sql: deadlock if not enough connections available
Please answer these questions before submitting your issue. Thanks!
- What version of Go are you using (
go version
)? 1.5.1 - What operating system and processor architecture are you using (
go env
)? GOARCH=“amd64” GOBIN=“” GOEXE=“” GOHOSTARCH=“amd64” GOHOSTOS=“linux” GOOS=“linux” GOPATH=“/home/yubai/go:/home/yubai/dev/s3/swiftmeter/:/home/yubai/dev/s3/imageserver/” GORACE=“” GOROOT=“/usr/lib/golang” GOTOOLDIR=“/usr/lib/golang/pkg/tool/linux_amd64” GO15VENDOREXPERIMENT=“” CC=“gcc” GOGCCFLAGS=“-fPIC -m64 -pthread -fmessage-length=0” CXX=“g++” CGO_ENABLED=“1” - What did you do? If possible, provide a recipe for reproducing the error. A complete runnable program is good. A link on play.golang.org is best.
package main
import (
"database/sql"
"fmt"
_ "s3lib/third/mysql"
"os"
)
const (
User string = "****"
Password string = "****"
Host string = "****"
Port int = ****
ReadTimeout string = "60s"
WriteTimeout string = "10s"
StmtInsertSQL string = "insert into t1 values (?, ?, ?);"
)
func main() {
dataSource := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8&interpolateParams=false&readTimeout=%s&writeTimeout=%s",
User, Password, Host, Port, "", ReadTimeout, WriteTimeout)
db, err := sql.Open("mysql", dataSource)
if err != nil {
os.Exit(-1)
}
db.SetMaxOpenConns(1)
db.SetMaxIdleConns(1)
tx, err := db.Begin()
if err != nil {
os.Exit(-1)
}
insert_stmt, err := db.Prepare(StmtInsertSQL) // deadlock here
if err != nil {
os.Exit(-1)
}
insert_stmt_r1 := tx.Stmt(insert_stmt)
insert_stmt_r1.Exec(10000, "hello", 100)
tx.Commit()
}
- What did you expect to see? success or got an “deadlock error”
- What did you see instead? The process deadlock while calling “Prepare”, it seems that, “Prepare” need an idle connection, but I have set the MaxOpenConns as 1, the transaction has occupied the only one connection so the Prepare deadlock. It can be worked around by setting MaxOpenConns greater than 1.
The deadlock call stack is: ··· 1 @ 0x42dbe3 0x42dca4 0x404ed1 0x40458b 0x4663e6 0x46746e 0x4672d6 0x4014ab 0x42d800 0x45e2b1 0x4663e6 database/sql.(_DB).conn+0x2f6 /usr/lib/golang/src/database/sql/sql.go:704 0x46746e database/sql.(_DB).prepare+0x4e /usr/lib/golang/src/database/sql/sql.go:867 0x4672d6 database/sql.(*DB).Prepare+0x76 /usr/lib/golang/src/database/sql/sql.go:849 0x4014ab main.main+0x4ab /home/yubai/dev/lks3plus/gocode/src/apps/test1/test1.go:33 0x42d800 runtime.main+0x2b0 /usr/lib/golang/src/runtime/proc.go:111 ···
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (6 by maintainers)
@base698 Please open a new issue, describe the exact situation you are seeing, show code if possible. Lots of things were said here and I’m unsure which part you are referring to.
Closed issues aren’t tracked.
i’m using go 1.8 and 1.8.1 and I see the behavior described.