mirror of
https://github.com/jackc/pgx.git
synced 2025-04-27 13:14:32 +00:00
stdlib.ReleaseConn closes connections left in invalid state
If a connection is in a transaction or has an open result set then close the connection when returning it to database/sql. When next database/sql attempts to use it the connection will return driver.ErrBadConn and database/sql will remove it from the pool. fixes #673
This commit is contained in:
parent
06c3181836
commit
77c1076d39
@ -570,6 +570,12 @@ func ReleaseConn(db *sql.DB, conn *pgx.Conn) error {
|
|||||||
var tx *sql.Tx
|
var tx *sql.Tx
|
||||||
var ok bool
|
var ok bool
|
||||||
|
|
||||||
|
if conn.PgConn().IsBusy() || conn.PgConn().TxStatus() != 'I' {
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||||
|
defer cancel()
|
||||||
|
conn.Close(ctx)
|
||||||
|
}
|
||||||
|
|
||||||
fakeTxMutex.Lock()
|
fakeTxMutex.Lock()
|
||||||
tx, ok = fakeTxConns[conn]
|
tx, ok = fakeTxConns[conn]
|
||||||
if ok {
|
if ok {
|
||||||
|
@ -692,6 +692,39 @@ func TestAcquireConn(t *testing.T) {
|
|||||||
ensureConnValid(t, db)
|
ensureConnValid(t, db)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://github.com/jackc/pgx/issues/673
|
||||||
|
func TestReleaseConnWithTxInProgress(t *testing.T) {
|
||||||
|
db := openDB(t)
|
||||||
|
defer closeDB(t, db)
|
||||||
|
|
||||||
|
c1, err := stdlib.AcquireConn(db)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
_, err = c1.Exec(context.Background(), "begin")
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
c1PID := c1.PgConn().PID()
|
||||||
|
|
||||||
|
err = stdlib.ReleaseConn(db, c1)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
c2, err := stdlib.AcquireConn(db)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
c2PID := c2.PgConn().PID()
|
||||||
|
|
||||||
|
err = stdlib.ReleaseConn(db, c2)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
require.NotEqual(t, c1PID, c2PID)
|
||||||
|
|
||||||
|
// Releasing a conn with a tx in progress should close the connection
|
||||||
|
stats := db.Stats()
|
||||||
|
require.Equal(t, 1, stats.OpenConnections)
|
||||||
|
|
||||||
|
ensureConnValid(t, db)
|
||||||
|
}
|
||||||
|
|
||||||
func TestConnPingContextSuccess(t *testing.T) {
|
func TestConnPingContextSuccess(t *testing.T) {
|
||||||
db := openDB(t)
|
db := openDB(t)
|
||||||
defer closeDB(t, db)
|
defer closeDB(t, db)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user