]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Opened up the checking for "disconnect" with psycopg2/libpq
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 22 Apr 2013 23:33:39 +0000 (19:33 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 22 Apr 2013 23:33:39 +0000 (19:33 -0400)
to check for all the various "disconnect" messages within
the full exception hierarchy.  Specifically the
"closed the connection unexpectedly" message has now been
seen in at least three different exception types.
[ticket:2712]

doc/build/changelog/changelog_08.rst
lib/sqlalchemy/dialects/postgresql/psycopg2.py

index 1f89984527849fbeb4d9a4dc8710b2bd7fec73cf..a3b7ffcb52fddb9975e56dc2c465bbe5ca2697d9 100644 (file)
@@ -6,6 +6,16 @@
 .. changelog::
     :version: 0.8.1
 
+    .. change::
+      :tags: bug, postgresql
+      :tickets: 2712
+
+      Opened up the checking for "disconnect" with psycopg2/libpq
+      to check for all the various "disconnect" messages within
+      the full exception hierarchy.  Specifically the
+      "closed the connection unexpectedly" message has now been
+      seen in at least three different exception types.
+
     .. change::
       :tags: bug, sql, mysql
       :tickets: 2682
index 1f118067fa7419c9a059c410fa0a51ed2b2f0751..f5e122a1bf4be0bfe8c4c73b26f867b555bfc848 100644 (file)
@@ -421,23 +421,26 @@ class PGDialect_psycopg2(PGDialect):
         return ([], opts)
 
     def is_disconnect(self, e, connection, cursor):
-        if isinstance(e, self.dbapi.OperationalError):
-            # these error messages from libpq: interfaces/libpq/fe-misc.c.
-            # TODO: these are sent through gettext in libpq and we can't
-            # check within other locales - consider using connection.closed
-            return 'terminating connection' in str(e) or \
-                    'closed the connection' in str(e) or \
-                    'connection not open' in str(e) or \
-                    'could not receive data from server' in str(e)
-        elif isinstance(e, self.dbapi.InterfaceError):
-            # psycopg2 client errors, psycopg2/conenction.h, psycopg2/cursor.h
-            return 'connection already closed' in str(e) or \
-                    'cursor already closed' in str(e)
-        elif isinstance(e, self.dbapi.ProgrammingError):
-            # not sure where this path is originally from, it may
-            # be obsolete.   It really says "losed", not "closed".
-            return "losed the connection unexpectedly" in str(e)
-        else:
-            return False
+        if isinstance(e, self.dbapi.Error):
+            str_e = str(e)
+            for msg in [
+                # these error messages from libpq: interfaces/libpq/fe-misc.c
+                # and interfaces/libpq/fe-secure.c.
+                # TODO: these are sent through gettext in libpq and we can't
+                # check within other locales - consider using connection.closed
+                'terminating connection',
+                'closed the connection',
+                'connection not open',
+                'could not receive data from server',
+                # psycopg2 client errors, psycopg2/conenction.h, psycopg2/cursor.h
+                'connection already closed',
+                'cursor already closed',
+                # not sure where this path is originally from, it may
+                # be obsolete.   It really says "losed", not "closed".
+                'losed the connection unexpectedly'
+            ]:
+                if msg in str_e:
+                    return True
+        return False
 
 dialect = PGDialect_psycopg2