mirror of https://github.com/jackc/pgx.git
Merge pull request #712 from lbcjbb/master
[pgxpool] Fix connection leak if BeginTx() failpull/715/head
commit
7a9efdefde
|
@ -429,6 +429,7 @@ func (p *Pool) BeginTx(ctx context.Context, txOptions pgx.TxOptions) (pgx.Tx, er
|
|||
|
||||
t, err := c.BeginTx(ctx, txOptions)
|
||||
if err != nil {
|
||||
c.Release()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
|
|
@ -555,3 +555,26 @@ func TestConnPoolQueryConcurrentLoad(t *testing.T) {
|
|||
<-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())
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue