diff --git a/neo4j/work/simple.py b/neo4j/work/simple.py index d2f17c0e6..1a73089db 100644 --- a/neo4j/work/simple.py +++ b/neo4j/work/simple.py @@ -26,11 +26,11 @@ sleep, ) -from neo4j.conf import SessionConfig from neo4j.api import ( READ_ACCESS, WRITE_ACCESS, ) +from neo4j.conf import SessionConfig from neo4j.data import DataHydrator from neo4j.exceptions import ( ClientError, @@ -337,7 +337,7 @@ def _run_transaction(self, access_mode, transaction_function, *args, **kwargs): try: result = transaction_function(tx, *args, **kwargs) except Exception: - tx.rollback() + tx.close() raise else: tx.commit() diff --git a/neo4j/work/transaction.py b/neo4j/work/transaction.py index 3ff5739fb..1061f5761 100644 --- a/neo4j/work/transaction.py +++ b/neo4j/work/transaction.py @@ -139,8 +139,8 @@ def commit(self): if self._closed: raise TransactionError("Transaction closed") metadata = {} - self._consume_results() # DISCARD pending records then do a commit. try: + self._consume_results() # DISCARD pending records then do a commit. self._connection.commit(on_success=metadata.update) self._connection.send_all() self._connection.fetch_all() @@ -159,18 +159,19 @@ def rollback(self): if self._closed: raise TransactionError("Transaction closed") metadata = {} - if not self._connection._is_reset: - self._consume_results() # DISCARD pending records then do a rollback. - self._connection.rollback(on_success=metadata.update) - self._connection.send_all() - self._connection.fetch_all() - self._closed = True - self._on_closed() + try: + if not self._connection._is_reset: + # DISCARD pending records then do a rollback. + self._consume_results() + self._connection.rollback(on_success=metadata.update) + self._connection.send_all() + self._connection.fetch_all() + finally: + self._closed = True + self._on_closed() def close(self): """Close this transaction, triggering a ROLLBACK if not closed. - - :raise TransactionError: if the transaction could not perform a ROLLBACK. """ if self._closed: return