diff --git a/pgconn.go b/pgconn.go index 8f3291f1..153829ca 100644 --- a/pgconn.go +++ b/pgconn.go @@ -463,10 +463,9 @@ func (pgConn *PgConn) hardClose() error { return pgConn.conn.Close() } -// TODO - rethink how to report status. At the moment this is just a temporary measure so pgx.Conn can detect death of -// underlying connection. -func (pgConn *PgConn) IsAlive() bool { - return pgConn.status >= connStatusIdle +// IsClosed reports if the connection has been closed. +func (pgConn *PgConn) IsClosed() bool { + return pgConn.status < connStatusIdle } // lock locks the connection. It panics if the connection is already locked or is closed. diff --git a/pgconn_test.go b/pgconn_test.go index 1cd74024..64628262 100644 --- a/pgconn_test.go +++ b/pgconn_test.go @@ -433,7 +433,7 @@ func TestConnExecContextCanceled(t *testing.T) { } err = multiResult.Close() assert.Equal(t, context.DeadlineExceeded, err) - assert.False(t, pgConn.IsAlive()) + assert.True(t, pgConn.IsClosed()) } func TestConnExecContextPrecanceled(t *testing.T) { @@ -566,7 +566,7 @@ func TestConnExecParamsCanceled(t *testing.T) { assert.Equal(t, pgconn.CommandTag(nil), commandTag) assert.Equal(t, context.DeadlineExceeded, err) - assert.False(t, pgConn.IsAlive()) + assert.True(t, pgConn.IsClosed()) } func TestConnExecParamsPrecanceled(t *testing.T) { @@ -692,7 +692,7 @@ func TestConnExecPreparedCanceled(t *testing.T) { commandTag, err := result.Close() assert.Equal(t, pgconn.CommandTag(nil), commandTag) assert.Equal(t, context.DeadlineExceeded, err) - assert.False(t, pgConn.IsAlive()) + assert.True(t, pgConn.IsClosed()) } func TestConnExecPreparedPrecanceled(t *testing.T) { @@ -1142,7 +1142,7 @@ func TestConnCopyToCanceled(t *testing.T) { assert.True(t, errors.Is(err, context.DeadlineExceeded)) assert.Equal(t, pgconn.CommandTag(nil), res) - assert.False(t, pgConn.IsAlive()) + assert.True(t, pgConn.IsClosed()) } func TestConnCopyToPrecanceled(t *testing.T) { @@ -1233,7 +1233,7 @@ func TestConnCopyFromCanceled(t *testing.T) { assert.Equal(t, int64(0), ct.RowsAffected()) assert.True(t, errors.Is(err, context.DeadlineExceeded)) - assert.False(t, pgConn.IsAlive()) + assert.True(t, pgConn.IsClosed()) } func TestConnCopyFromPrecanceled(t *testing.T) {