From c5587fda7986df5851491a069830ddd4a63e01ba Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 2 May 2021 18:31:03 -0400 Subject: unify transactional context managers Applied consistent behavior to the use case of calling ``.commit()`` or ``.rollback()`` inside of an existing ``.begin()`` context manager, with the addition of potentially emitting SQL within the block subsequent to the commit or rollback. This change continues upon the change first added in :ticket:`6155` where the use case of calling "rollback" inside of a ``.begin()`` contextmanager block was proposed: * calling ``.commit()`` or ``.rollback()`` will now be allowed without error or warning within all scopes, including that of legacy and future :class:`_engine.Engine`, ORM :class:`_orm.Session`, asyncio :class:`.AsyncEngine`. Previously, the :class:`_orm.Session` disallowed this. * The remaining scope of the context manager is then closed; when the block ends, a check is emitted to see if the transaction was already ended, and if so the block returns without action. * It will now raise **an error** if subsequent SQL of any kind is emitted within the block, **after** ``.commit()`` or ``.rollback()`` is called. The block should be closed as the state of the executable object would otherwise be undefined in this state. Fixes: #6288 Change-Id: I8b21766ae430f0fa1ac5ef689f4c0fb19fc84336 --- lib/sqlalchemy/future/engine.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'lib/sqlalchemy/future') diff --git a/lib/sqlalchemy/future/engine.py b/lib/sqlalchemy/future/engine.py index cee17a432..ab890ca4f 100644 --- a/lib/sqlalchemy/future/engine.py +++ b/lib/sqlalchemy/future/engine.py @@ -359,16 +359,12 @@ class Engine(_LegacyEngine): def __enter__(self): self.transaction = self.conn.begin() + self.transaction.__enter__() return self.conn def __exit__(self, type_, value, traceback): try: - if type_ is not None: - if self.transaction.is_active: - self.transaction.rollback() - else: - if self.transaction.is_active: - self.transaction.commit() + self.transaction.__exit__(type_, value, traceback) finally: self.conn.close() -- cgit v1.2.1