database/sql: Why do DB.QueryContext and Stmt.QueryContext have different retry logic? #20433
Labels
FrozenDueToAge
NeedsInvestigation
Someone must examine and confirm this is a valid issue and not a duplicate of an existing one.
Milestone
Please answer these questions before submitting your issue. Thanks!
What version of Go are you using (
go version
)?go version go1.8.1 darwin/amd64
What operating system and processor architecture are you using (
go env
)?Summary
We are getting problems with
databse/sql/driver.ErrBadConn
, but only if we use the prepared statements. It seems to me that this is because*DB.QueryContext
retries with a fresh connection and*Stmt.QueryContext
does not. I want to verify whether this is a bug or an expected behaviour,When
*DB.QueryContext
receivesdriver.ErrBaddConn
from the driver, it will retry twice (maxBadConnRetries
) using cached connection (cachedOrNewConn
strategy). If this fails, it will retry once more using new connection (alwaysNewConn strategy). The code is here: https://golang.org/src/database/sql/sql.go?s=7997:9207#L1223*Stmt.QueryContext
will also retry twice (againmaxBadConnRetries
) using the cached connection. If this fails, it will returndriver.ErrBadConn
, without retrying using the new connection. The code is here: https://golang.org/src/database/sql/sql.go?s=7997:9207#L1935s.connStmt
usescachedOrNewConn
strategy.I think this is a bug (but I will be happy to hear otherwise), introduced in this commit: c468f94
Before this commit,
maxBadConnRetries
was 10 and retries where alway done using old (cached) connections. Commit changed the logic, decreasing the number of retires and adding one more retry on fresh connection. While decrease was global (both*DB
and*Stmt
where affected), retry-using-fresh-connection logic was applied only to*DB
.I first posted this on golang-nuts and then followed the instructions to post it here.
I have been looking at the code in
datbase/sql
quite a lot lately, so if this is a bug, I think I can prepare the patch.The text was updated successfully, but these errors were encountered: