[pgxpool] Fix connection leak if BeginTx() fail
This commit is contained in:
@@ -429,6 +429,7 @@ func (p *Pool) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, er
|
|||||||
|
|
||||||
t, err := c.BeginTx(ctx, txOptions)
|
t, err := c.BeginTx(ctx, txOptions)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
c.Release()
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -555,3 +555,26 @@ func TestConnPoolQueryConcurrentLoad(t *testing.T) {
|
|||||||
<-done
|
<-done
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnReleaseWhenBeginFail(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
db, err := pgxpool.Connect(ctx, os.Getenv("PGX_TEST_DATABASE"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
tx, err := db.BeginTx(ctx, pgx.TxOptions{
|
||||||
|
IsoLevel: pgx.TxIsoLevel("foo"),
|
||||||
|
})
|
||||||
|
assert.Error(t, err)
|
||||||
|
if !assert.Zero(t, tx) {
|
||||||
|
err := tx.Rollback(ctx)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
stats := db.Stat()
|
||||||
|
assert.EqualValues(t, 0, stats.TotalConns())
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user