Skip to content

Commit 79de84b

Browse files
committed
Actively unset reset agent in discard transaction
The assumptions in _discard_transaction from 916e1fe were too narrow, assuming that if the given transaction were not our "current" one, that this would not be the reset agent. however as the legacy behvaior is that even a "nested" transaction gets set as "self._transaction", this did not accommodate for the nested transaction being thrown away. We will attempt to refine all of this logic in sqlalchemy#5327 for 1.4 /master assuming this is feasible for the full suite of current use cases. Fixes: sqlalchemy#5326 Change-Id: I6787e82c9e50c23317f87d0d094122c6a6f066da
1 parent 6bd171c commit 79de84b

3 files changed

Lines changed: 27 additions & 16 deletions

File tree

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.. change::
2+
:tags: bug, engine
3+
:tickets: 5326
4+
5+
Further refinements to the fixes to the "reset" agent fixed in
6+
:ticket:`5326`, which now emits a warning when it is not being correctly
7+
invoked and corrects for the behavior. Additional scenarios have been
8+
identified and fixed where this warning was being emitted.
9+

lib/sqlalchemy/engine/base.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -821,25 +821,13 @@ def _discard_transaction(self, trans):
821821
assert trans._parent is trans
822822
self._transaction = None
823823

824-
# test suite w/ SingletonThreadPool will have cases
825-
# where _reset_agent is on a different Connection
826-
# entirely so we can't assert this here.
827-
# if (
828-
# not self._is_future
829-
# and self._still_open_and_connection_is_valid
830-
# ):
831-
# assert self.__connection._reset_agent is None
832824
else:
833825
assert trans._parent is not trans
834826
self._transaction = trans._parent
835827

836-
# not doing this assertion for now, however this is how
837-
# it would look:
838-
# if self._still_open_and_connection_is_valid:
839-
# trans = self._transaction
840-
# while not trans._is_root:
841-
# trans = trans._parent
842-
# assert self.__connection._reset_agent is trans
828+
if not self._is_future and self._still_open_and_connection_is_valid:
829+
if self.__connection._reset_agent is trans:
830+
self.__connection._reset_agent = None
843831

844832
def _rollback_to_savepoint_impl(
845833
self, name, context, deactivate_only=False

test/engine/test_transaction.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -663,7 +663,7 @@ def test_trans_commit_reset_agent_broken_ensure(self):
663663
conn.close()
664664

665665
@testing.requires.savepoints
666-
def test_begin_nested_trans_close(self):
666+
def test_begin_nested_trans_close_one(self):
667667
with testing.db.connect() as connection:
668668
t1 = connection.begin()
669669
assert connection.connection._reset_agent is t1
@@ -677,6 +677,20 @@ def test_begin_nested_trans_close(self):
677677
assert connection.connection._reset_agent is None
678678
assert not t1.is_active
679679

680+
@testing.requires.savepoints
681+
def test_begin_nested_trans_close_two(self):
682+
with testing.db.connect() as connection:
683+
t1 = connection.begin()
684+
assert connection.connection._reset_agent is t1
685+
t2 = connection.begin_nested()
686+
assert connection.connection._reset_agent is t1
687+
assert connection._transaction is t2
688+
689+
assert connection.connection._reset_agent is t1
690+
t1.close()
691+
assert connection.connection._reset_agent is None
692+
assert not t1.is_active
693+
680694
@testing.requires.savepoints
681695
def test_begin_nested_trans_rollback(self):
682696
with testing.db.connect() as connection:

0 commit comments

Comments
 (0)