Is there a reason why Ping is called when starting a new transaction?
NewDatabaseTx clones db with checkConn = true
|
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.
Is there a reason why
Pingis called when starting a new transaction?NewDatabaseTxclones db withcheckConn= truedb/mysql/database.go
Lines 219 to 220 in ca469a8
So
Pingis called beforebegin transaction:db/internal/sqladapter/database.go
Lines 330 to 334 in ca469a8
The problem is that
Pingis not retried if ping returnsdriver.BadConnection:https://github.com/golang/go/blob/3bf9b77c0c8f123728fcfc802599232e9bd95476/src/database/sql/sql.go#L724-L741
Whereas
begin transactionretries: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()orExec()in the same situation works fine.