You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are seeing "invalid connection" errors when trying to use MySQL connections wrapped by otelsql.
This type driver.Validator is implemented for MySQL connection pointers, and the database/sql connection pool wants to check IsValid on that connection in putConn. The otel wrapper (*otConn) does not implement driver.Validator, so the connections are always considered valid.
Proposed Solution
Implement driver.Validator, with default to true when the wrapped connection does not implement driver.Validator
And a query is in-flight when its context is cancelled:
ctx, cancel:=context.WithTimeout(context.Background(), 100*time.Millisecond)
defercancel()
// If this query is in-flight when the context is cancelled,// the underlying mysql connection is marked invalid.rows, err:=db.QueryContext(ctx, "SELECT SLEEP(10)")
The underlying MySQL driver marks that connection as invalid.
Normally, database/sql would call IsValid() before handing the connection back out, and discard it. But because the pool contains *otConn values, not the raw MySQL driver type, and *otConn doesn't implement driver.Validator, the check is skipped entirely. The next caller to get that connection from the pool hits an error:
// database/sql hands out the same (invalid) connection.// It would normally call IsValid() and discard it, but *otConn// doesn't implement driver.Validator, so the check is skipped.rows, err:=db.QueryContext(context.Background(), "SELECT 1")
// err: "invalid connection"
Proposed fix — delegate to the underlying connection's IsValid():
This fallback is justified because it matches the existing default behavior: if a connection does not implement driver.Validator, Go's database/sql pool already treats it as valid.
Activity
XSAM commentedon Apr 7, 2026
Hey @Twister915, thanks for the issue. Could you provide a code snippet that reproduces this issue?
Twister915 commentedon Apr 7, 2026
Hey @XSAM, sure, here's what we're seeing.
If you open a connection pool through otelsql:
And a query is in-flight when its context is cancelled:
The underlying MySQL driver marks that connection as invalid.
Normally,
database/sqlwould callIsValid()before handing the connection back out, and discard it. But because the pool contains*otConnvalues, not the raw MySQL driver type, and*otConndoesn't implementdriver.Validator, the check is skipped entirely. The next caller to get that connection from the pool hits an error:Proposed fix — delegate to the underlying connection's
IsValid():This fallback is justified because it matches the existing default behavior: if a connection does not implement
driver.Validator, Go'sdatabase/sqlpool already treats it as valid.The relevant stdlib code in
database/sqlis:Hope that helps!
implement driver.Validator on otConn (#619)