Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

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

#200

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-
psycopg2.extensions.STATUS_READY
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.

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

State: resolved
Milestone: none
Assigned to: nobody

Activity

  1. Daniele Varrazzo
    Daniele Varrazzo

    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.

    March 25th, 2014 @ 08:43 AM

  2. Agniva De Sarkar
    Agniva De Sarkar

    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.

    April 2nd, 2014 @ 07:23 AM

  3. Daniele Varrazzo
    Daniele Varrazzo

    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.

    April 2nd, 2014 @ 08:09 AM

  4. Daniele Varrazzo
    Daniele Varrazzo
    • State changed from new to resolved

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

    http://initd.org/psycopg/docs/connection.html#connection.status

    April 3rd, 2014 @ 01:11 AM