From: Daniele Varrazzo Date: Sun, 21 Feb 2021 03:20:38 +0000 (+0100) Subject: Don't try to rollback on exit if the connection is bad X-Git-Tag: 3.0.dev0~87^2~60 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=8225d970000780bb410c4471b61336045aaa49f0;p=thirdparty%2Fpsycopg.git Don't try to rollback on exit if the connection is bad --- diff --git a/psycopg3/psycopg3/connection.py b/psycopg3/psycopg3/connection.py index d6af27d43..8ed35f7f9 100644 --- a/psycopg3/psycopg3/connection.py +++ b/psycopg3/psycopg3/connection.py @@ -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()