diff options
| author | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-01-15 17:23:52 -0500 | 
|---|---|---|
| committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2021-01-15 17:51:34 -0500 | 
| commit | f898ef162da65a3755d25ee194885677c880c91f (patch) | |
| tree | 2d45d310f62fd53bb69e73285b2d95f0e2f6c70e /lib/sqlalchemy/dialects/postgresql | |
| parent | 5be3c030b321ecc342bf4cd84cbb0838efacd155 (diff) | |
| download | sqlalchemy-f898ef162da65a3755d25ee194885677c880c91f.tar.gz | |
run handle error for commit/rollback fail and cancel transaction
Fixed bug in asyncpg dialect where a failure during a "commit" or less
likely a "rollback" should cancel the entire transaction; it's no longer
possible to emit rollback. Previously the connection would continue to
await a rollback that could not succeed as asyncpg would reject it.
Fixes: #5824
Change-Id: I5a4916740c269b410f4d1a78ed25191de344b9d0
Diffstat (limited to 'lib/sqlalchemy/dialects/postgresql')
| -rw-r--r-- | lib/sqlalchemy/dialects/postgresql/asyncpg.py | 24 | 
1 files changed, 18 insertions, 6 deletions
diff --git a/lib/sqlalchemy/dialects/postgresql/asyncpg.py b/lib/sqlalchemy/dialects/postgresql/asyncpg.py index e542c77f4..424ed0d50 100644 --- a/lib/sqlalchemy/dialects/postgresql/asyncpg.py +++ b/lib/sqlalchemy/dialects/postgresql/asyncpg.py @@ -615,6 +615,10 @@ class AsyncAdapt_asyncpg_connection:          return prepared_stmt, attributes      def _handle_exception(self, error): +        if self._connection.is_closed(): +            self._transaction = None +            self._started = False +          if not isinstance(error, AsyncAdapt_asyncpg_dbapi.Error):              exception_mapping = self.dbapi._asyncpg_error_translate @@ -669,15 +673,23 @@ class AsyncAdapt_asyncpg_connection:      def rollback(self):          if self._started: -            self.await_(self._transaction.rollback()) -            self._transaction = None -            self._started = False +            try: +                self.await_(self._transaction.rollback()) +            except Exception as error: +                self._handle_exception(error) +            finally: +                self._transaction = None +                self._started = False      def commit(self):          if self._started: -            self.await_(self._transaction.commit()) -            self._transaction = None -            self._started = False +            try: +                self.await_(self._transaction.commit()) +            except Exception as error: +                self._handle_exception(error) +            finally: +                self._transaction = None +                self._started = False      def close(self):          self.rollback()  | 
