use connection.closed, cursor.closed for disconnect detection when possible

Issue #2045 resolved
Mike Bayer repo owner created an issue

libpq offers different messages based on locale so our approach does not scale. Need to remove the implicit "invalidate" in pool.py in order to have connection/cursor available in is_disconnect()

Comments (2)

  1. Mike Bayer reporter

    so much for that idea:

    >>> conn = psycopg2.connect(user='scott', password='tiger', database='test')
    >>> conn.closed
    0
    >>> conn.execute("select 1")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    AttributeError: 'psycopg2._psycopg.connection' object has no attribute 'execute'
    >>> cursor = conn.cursor()
    >>> cursor.execute("select 1")
    
    ### <<< stop the pg database here >>> ###
    
    >>> conn.closed
    0
    >>> cursor.execute("select 1")
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    psycopg2.OperationalError: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.
    
    >>> conn.closed
    0
    >>> cursor.closed
    False
    >>>
    
  2. Log in to comment