mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
database/sql: add Pinger interface to driver Conn
Change-Id: If6eb3a7c9ad48a517e584567b1003479c1df6cca Reviewed-on: https://go-review.googlesource.com/32136 Reviewed-by: Daniel Theophanes <kardianos@gmail.com> Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
398e861d97
commit
4b90b7a28a
3 changed files with 73 additions and 6 deletions
|
|
@ -553,15 +553,27 @@ func Open(driverName, dataSourceName string) (*DB, error) {
|
|||
// PingContext verifies a connection to the database is still alive,
|
||||
// establishing a connection if necessary.
|
||||
func (db *DB) PingContext(ctx context.Context) error {
|
||||
// TODO(bradfitz): give drivers an optional hook to implement
|
||||
// this in a more efficient or more reliable way, if they
|
||||
// have one.
|
||||
dc, err := db.conn(ctx, cachedOrNewConn)
|
||||
var dc *driverConn
|
||||
var err error
|
||||
|
||||
for i := 0; i < maxBadConnRetries; i++ {
|
||||
dc, err = db.conn(ctx, cachedOrNewConn)
|
||||
if err != driver.ErrBadConn {
|
||||
break
|
||||
}
|
||||
}
|
||||
if err == driver.ErrBadConn {
|
||||
dc, err = db.conn(ctx, alwaysNewConn)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
db.putConn(dc, nil)
|
||||
return nil
|
||||
|
||||
if pinger, ok := dc.ci.(driver.Pinger); ok {
|
||||
err = pinger.Ping(ctx)
|
||||
}
|
||||
db.putConn(dc, err)
|
||||
return err
|
||||
}
|
||||
|
||||
// Ping verifies a connection to the database is still alive,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue