]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
Don't try to rollback on exit if the connection is bad
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 21 Feb 2021 03:20:38 +0000 (04:20 +0100)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Fri, 12 Mar 2021 04:07:25 +0000 (05:07 +0100)
psycopg3/psycopg3/connection.py

index d6af27d4306be4f4831264e16ee7f28e9bb54fa1..8ed35f7f9b141155ffb341854ff76dbb0e2c46bd 100644 (file)
@@ -460,13 +460,14 @@ class Connection(BaseConnection):
         if exc_type:
             # try to rollback, but if there are problems (connection in a bad
             # state) just warn without clobbering the exception bubbling up.
-            try:
-                self.rollback()
-            except Exception as exc2:
-                warnings.warn(
-                    f"error rolling back the transaction on {self}: {exc2}",
-                    RuntimeWarning,
-                )
+            if not self.closed:
+                try:
+                    self.rollback()
+                except Exception as exc2:
+                    warnings.warn(
+                        f"error rolling back the transaction on {self}: {exc2}",
+                        RuntimeWarning,
+                    )
         else:
             self.commit()
 
@@ -632,13 +633,14 @@ class AsyncConnection(BaseConnection):
         if exc_type:
             # try to rollback, but if there are problems (connection in a bad
             # state) just warn without clobbering the exception bubbling up.
-            try:
-                await self.rollback()
-            except Exception as exc2:
-                warnings.warn(
-                    f"error rolling back the transaction on {self}: {exc2}",
-                    RuntimeWarning,
-                )
+            if not self.closed:
+                try:
+                    await self.rollback()
+                except Exception as exc2:
+                    warnings.warn(
+                        f"error rolling back the transaction on {self}: {exc2}",
+                        RuntimeWarning,
+                    )
         else:
             await self.commit()