Skip to content

Commit aeaf7a7

Browse files
authored
Don't try to rollback a closed transaction (#531)
* Don't try to rollback a closed transaction * Clean up transaction error handling
1 parent 6619a51 commit aeaf7a7

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

neo4j/work/simple.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@
2626
sleep,
2727
)
2828

29-
from neo4j.conf import SessionConfig
3029
from neo4j.api import (
3130
READ_ACCESS,
3231
WRITE_ACCESS,
3332
)
33+
from neo4j.conf import SessionConfig
3434
from neo4j.data import DataHydrator
3535
from neo4j.exceptions import (
3636
ClientError,
@@ -337,7 +337,7 @@ def _run_transaction(self, access_mode, transaction_function, *args, **kwargs):
337337
try:
338338
result = transaction_function(tx, *args, **kwargs)
339339
except Exception:
340-
tx.rollback()
340+
tx.close()
341341
raise
342342
else:
343343
tx.commit()

neo4j/work/transaction.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ def commit(self):
139139
if self._closed:
140140
raise TransactionError("Transaction closed")
141141
metadata = {}
142-
self._consume_results() # DISCARD pending records then do a commit.
143142
try:
143+
self._consume_results() # DISCARD pending records then do a commit.
144144
self._connection.commit(on_success=metadata.update)
145145
self._connection.send_all()
146146
self._connection.fetch_all()
@@ -159,18 +159,19 @@ def rollback(self):
159159
if self._closed:
160160
raise TransactionError("Transaction closed")
161161
metadata = {}
162-
if not self._connection._is_reset:
163-
self._consume_results() # DISCARD pending records then do a rollback.
164-
self._connection.rollback(on_success=metadata.update)
165-
self._connection.send_all()
166-
self._connection.fetch_all()
167-
self._closed = True
168-
self._on_closed()
162+
try:
163+
if not self._connection._is_reset:
164+
# DISCARD pending records then do a rollback.
165+
self._consume_results()
166+
self._connection.rollback(on_success=metadata.update)
167+
self._connection.send_all()
168+
self._connection.fetch_all()
169+
finally:
170+
self._closed = True
171+
self._on_closed()
169172

170173
def close(self):
171174
"""Close this transaction, triggering a ROLLBACK if not closed.
172-
173-
:raise TransactionError: if the transaction could not perform a ROLLBACK.
174175
"""
175176
if self._closed:
176177
return

0 commit comments

Comments
 (0)