Skip to content

Implement driver.Validator #618

Closed
Closed

Description

@Twister915

Problem Statement

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

Activity

  1. added
    enhancement
    on Apr 6, 2026
  2. XSAM commented on Apr 7, 2026

    @XSAM
    Owner

    Hey @Twister915, thanks for the issue. Could you provide a code snippet that reproduces this issue?

  3. Twister915 commented on Apr 7, 2026

    @Twister915
    ContributorAuthor

    Hey @XSAM, sure, here's what we're seeing.

    If you open a connection pool through otelsql:

    db, err := otelsql.Open("mysql", "user:password@tcp(localhost:3306)/mydb")
    if err != nil {
        log.Fatal(err)
    }

    And a query is in-flight when its context is cancelled:

    ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
    defer cancel()
    
    // 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():

    func (c *otConn) IsValid() bool {
        if v, ok := c.Conn.(driver.Validator); ok {
            return v.IsValid()
        }
        return true
    }

    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.

    The relevant stdlib code in database/sql is:

    if cv, ok := dc.ci.(driver.Validator); ok {
        return cv.IsValid()
    }
    return true

    Hope that helps!

  4. added a commit that references this issue on Apr 8, 2026
  5. added this to the v0.43.0 milestone on Apr 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions