This project is archived and is in readonly mode.

#200 ✓resolved
Agniva De Sarkar

connection.status is equal to STATUS_READY even when connection is closed

Reported by Agniva De Sarkar | March 18th, 2014 @ 08:55 AM

Steps to repro-
In [2]: db_conn = psycopg2.connect("dsn")

In [5]: psycopg2.extensions.STATUS_READY == db_conn.status
Out[5]: True
-- This is expected

In [6]: db_conn.closed
Out[6]: 0
-- This is also expected

In [7]: db_conn.close()

In [8]: psycopg2.extensions.STATUS_READY == db_conn.status
Out[8]: True
-- This is not expected. Why is this still True ??

From the documentation-
Connection established. No transaction in progress.

Connection is closed here. How can this be STATUS_READY ?

In [9]: db_conn.closed
Out[9]: 1
-- Expected.

Comments and changes to this ticket

  • Daniele Varrazzo

    Daniele Varrazzo March 25th, 2014 @ 08:43 AM

    Well, it's just that the status is no more updated after the connection is closed. We don't even have a STATUS_CLOSED. So we could say that's the "status of the connection when it's working, otherwise it's undefined". I'll write a line in the docs about that.

  • Agniva De Sarkar

    Agniva De Sarkar April 2nd, 2014 @ 07:23 AM

    thank you. On another note, how difficult is it to have a STATUS_CLOSED or STATUS_UNHEALTHY for a connection ? I came across this bug because I needed to write a reconnect function and for that I needed to verify whether the existing connection is good or bad.

  • Daniele Varrazzo

    Daniele Varrazzo April 2nd, 2014 @ 08:09 AM

    It is complicated and it is a duplication of other attributes. Now I'm fixing the bugs with the current statuses but I don't want to introduce more.

    The most reliable way to get the state of the connection is to call get_transaction_status() that will query the underlying connection instead of being a psycopg status. Note that until the currently released version of psycopg you cannot call that function when the connection is closed: this is not necessary as the underlying libpq function is local: starting from next version it will be possible to call it on closed connection too (it would probably return "unknown").

    So for now: check closed, if not closed check get_transaction_status(). This should be reliable.

  • Daniele Varrazzo

    Daniele Varrazzo April 3rd, 2014 @ 01:11 AM

    • State changed from “new” to “resolved”

    Documentation clarified, but there will be no change in the attribute behaviour.

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

<b>WARNING:</b> the informations in this tracker are archived. Please submit new tickets or comments to <a href="">the new tracker</a>.
Psycopg is the most used PostgreSQL adapter for the Python programming language. At the core it fully implements the Python DB API 2.0 specifications. Several extensions allow access to many of the features offered by PostgreSQL.

Shared Ticket Bins