Skip to content

mysql: "packets.go:144: bad connection" #469

@iliacimpoes

Description

@iliacimpoes

Is there a reason why Ping is called when starting a new transaction?

NewDatabaseTx clones db with checkConn = true

db/mysql/database.go

Lines 219 to 220 in ca469a8

func (d *database) NewDatabaseTx(ctx context.Context) (sqladapter.DatabaseTx, error) {
clone, err := d.clone(ctx, true)

So Ping is called before begin transaction:

if checkConn {
if err := nd.Ping(); err != nil {
return nil, err
}
}

The problem is that Ping is not retried if ping returns driver.BadConnection:
https://github.com/golang/go/blob/3bf9b77c0c8f123728fcfc802599232e9bd95476/src/database/sql/sql.go#L724-L741

Whereas begin transaction retries:
https://github.com/golang/go/blob/3bf9b77c0c8f123728fcfc802599232e9bd95476/src/database/sql/sql.go#L1619-L1632

We have a problem if a connection is idle for a long time and we start a transaction, it fails(because of a timeout). Whereas calling Query() or Exec() in the same situation works fine.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions