mirror of https://github.com/jackc/pgx.git
ConnPool.Begin retry logic checks connection IsAlive
ErrDeadConn is returned when calling an already dead connection. But the initial failure returns the real error. So we check for IsAlive instead of ErrDeadConn. Added test for ConnPool.Begin retry logic.pull/95/head
parent
6e5fa60c4c
commit
93aa2b2e80
14
conn_pool.go
14
conn_pool.go
|
@ -228,13 +228,19 @@ func (p *ConnPool) BeginIso(iso string) (*Tx, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
tx, err := c.BeginIso(iso)
|
tx, err := c.BeginIso(iso)
|
||||||
if err == ErrDeadConn {
|
if err != nil {
|
||||||
|
alive := c.IsAlive()
|
||||||
p.Release(c)
|
p.Release(c)
|
||||||
|
|
||||||
|
// If connection is still alive then the error is not something trying
|
||||||
|
// again on a new connection would fix, so just return the error. But
|
||||||
|
// if the connection is dead try to acquire a new connection and try
|
||||||
|
// again.
|
||||||
|
if alive {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
p.Release(c)
|
|
||||||
return nil, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
tx.pool = p
|
tx.pool = p
|
||||||
|
|
|
@ -363,6 +363,9 @@ func TestConnPoolTransactionIso(t *testing.T) {
|
||||||
func TestConnPoolBeginRetry(t *testing.T) {
|
func TestConnPoolBeginRetry(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
|
// Run timing sensitive test many times
|
||||||
|
for i := 0; i < 50; i++ {
|
||||||
|
func() {
|
||||||
pool := createConnPool(t, 2)
|
pool := createConnPool(t, 2)
|
||||||
defer pool.Close()
|
defer pool.Close()
|
||||||
|
|
||||||
|
@ -399,6 +402,8 @@ func TestConnPoolBeginRetry(t *testing.T) {
|
||||||
if txPid == victimConn.Pid {
|
if txPid == victimConn.Pid {
|
||||||
t.Error("Expected txPid to defer from killed conn pid, but it didn't")
|
t.Error("Expected txPid to defer from killed conn pid, but it didn't")
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestConnPoolQuery(t *testing.T) {
|
func TestConnPoolQuery(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue