Skip to content

Commit 20a15f9

Browse files
committed
Properly reset the transaction flag
1 parent 9cea4de commit 20a15f9

4 files changed

Lines changed: 36 additions & 1 deletion

File tree

dbutils/steady_db.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,7 @@ def tough_method(*args, **kwargs):
758758
with suppress(Exception):
759759
con2.close()
760760
if transaction:
761-
self._transaction = False
761+
con._transaction = False
762762
raise error # re-raise the original error again
763763
else:
764764
con._usage += 1

docs/changelog.html

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ <h2>3.2.0</h2>
2828
<li><p>Added <span class="docutils literal">dbapi_connection</span> and <span class="docutils literal">dbapi_cursor</span> attributes providing
2929
supported access to the underlying DB-API 2 objects.</p></li>
3030
</ul>
31+
<p>Bugfixes:</p>
32+
<ul class="simple">
33+
<li><p>Fixed the transaction flag being reset on the cursor instead of the
34+
connection when a connection could not be reopened during a transaction,
35+
which kept the failover mechanism suspended.</p></li>
36+
</ul>
3137
</section>
3238
<section id="section-2">
3339
<h2>3.1.2</h2>

docs/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ Changes:
2020
* Added ``dbapi_connection`` and ``dbapi_cursor`` attributes providing
2121
supported access to the underlying DB-API 2 objects.
2222

23+
Bugfixes:
24+
25+
* Fixed the transaction flag being reset on the cursor instead of the
26+
connection when a connection could not be reopened during a transaction,
27+
which kept the failover mechanism suspended.
28+
2329
3.1.2
2430
=====
2531

tests/test_steady_db.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,29 @@ def test_rollback_error():
730730
assert db._con.valid
731731

732732

733+
def test_transaction_ended_when_reopening_fails():
734+
"""A transaction that cannot be reopened must not stay active.
735+
736+
Otherwise the failover mechanism would stay suspended on the
737+
connection, so that it could never recover by itself again.
738+
"""
739+
db = steady_db_connect(dbapi, database='ok')
740+
cursor = db.cursor()
741+
db.begin()
742+
assert db._transaction
743+
# the database goes away and cannot be reached again
744+
db._kwargs['database'] = 'error'
745+
db._con.valid = cursor._cursor.valid = False
746+
with pytest.raises(dbapi.InternalError):
747+
cursor.execute('select test')
748+
assert not db._transaction
749+
# when the database is back, the failover mechanism must work again
750+
db._kwargs['database'] = 'ok'
751+
cursor.execute('select test')
752+
assert cursor.fetchone() == 'test'
753+
assert db._con.valid
754+
755+
733756
def timeout_is_not_fatal(error):
734757
"""A deliberate server side timeout does not break the connection."""
735758
return not error.args or error.args[0] != 3024

0 commit comments

Comments
 (0)