Skip to content

Commit cfec2c4

Browse files
committed
Rolled commit and rollback into parent functions
1 parent b3c2b74 commit cfec2c4

File tree

1 file changed

+30
-51
lines changed

1 file changed

+30
-51
lines changed

neo4j/__init__.py

Lines changed: 30 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class RoutingDriver(Driver):
232232

233233
def __new__(cls, uri, **config):
234234
from neobolt.addressing import SocketAddress
235-
from neobolt.bolt.connection import DEFAULT_PORT
235+
from neobolt.bolt.connection import DEFAULT_PORT, connect
236236
from neobolt.routing import RoutingConnectionPool
237237
from neobolt.security import SecurityPlan
238238
cls._check_uri(uri)
@@ -247,7 +247,6 @@ def __new__(cls, uri, **config):
247247
raise ValueError("TRUST_ON_FIRST_USE is not compatible with routing")
248248

249249
def connector(address, error_handler):
250-
from neobolt.bolt.connection import connect
251250
return connect(address, security_plan.ssl_context, error_handler, **config)
252251

253252
pool = RoutingConnectionPool(connector, initial_address, routing_context, initial_address, **config)
@@ -337,10 +336,9 @@ def __init__(self, acquirer, access_mode, **parameters):
337336
pass # for compatibility
338337

339338
def __del__(self):
340-
from neobolt.exceptions import ServiceUnavailable
341339
try:
342340
self.close()
343-
except (SessionError, ServiceUnavailable):
341+
except:
344342
pass
345343

346344
def __enter__(self):
@@ -519,10 +517,7 @@ def last_bookmark(self):
519517
def has_transaction(self):
520518
return bool(self._transaction)
521519

522-
def _create_transaction(self):
523-
self._transaction = Transaction(self, on_close=self._destroy_transaction)
524-
525-
def _destroy_transaction(self):
520+
def _close_transaction(self):
526521
self._transaction = None
527522

528523
def begin_transaction(self, bookmark=None):
@@ -546,21 +541,26 @@ def begin_transaction(self, bookmark=None):
546541
_warned_about_transaction_bookmarks = True
547542
self._bookmarks = [bookmark]
548543

549-
self._create_transaction()
550-
self._connect()
551-
self._begin()
552-
return self._transaction
544+
return self._open_transaction()
553545

554546
def commit_transaction(self):
555547
""" Commit the current transaction.
556548
557549
:returns: the bookmark returned from the server, if any
558550
:raise: :class:`.TransactionError` if no transaction is currently open
559551
"""
560-
if not self.has_transaction():
552+
self._assert_open()
553+
if not self._transaction:
561554
raise TransactionError("No transaction to commit")
562-
self._transaction = None
563-
bookmark = self._commit()
555+
metadata = {}
556+
try:
557+
cx = self._connection
558+
cx.run(u"COMMIT", {}, metadata)
559+
cx.pull_all(metadata)
560+
finally:
561+
self._disconnect(sync=True)
562+
self._transaction = None
563+
bookmark = metadata.get("bookmark")
564564
self._bookmarks = [bookmark]
565565
return bookmark
566566

@@ -569,17 +569,17 @@ def rollback_transaction(self):
569569
570570
:raise: :class:`.TransactionError` if no transaction is currently open
571571
"""
572-
from neobolt.exceptions import ServiceUnavailable
573-
if not self.has_transaction():
572+
self._assert_open()
573+
if not self._transaction:
574574
raise TransactionError("No transaction to rollback")
575-
self._destroy_transaction()
576-
self._rollback()
577-
578-
def reset(self):
579-
""" Reset the session.
580-
"""
581-
self._destroy_transaction()
582-
self._connection.reset()
575+
metadata = {}
576+
try:
577+
cx = self._connection
578+
cx.run(u"ROLLBACK", {}, metadata)
579+
cx.pull_all(metadata)
580+
finally:
581+
self._disconnect(sync=True)
582+
self._transaction = None
583583

584584
def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
585585
from neobolt.exceptions import ConnectionExpired, TransientError, ServiceUnavailable
@@ -593,10 +593,7 @@ def _run_transaction(self, access_mode, unit_of_work, *args, **kwargs):
593593
t0 = perf_counter()
594594
while True:
595595
try:
596-
self._create_transaction()
597-
self._connect(access_mode)
598-
self._begin()
599-
tx = self._transaction
596+
tx = self._open_transaction(access_mode)
600597
try:
601598
result = unit_of_work(tx, *args, **kwargs)
602599
except:
@@ -656,7 +653,9 @@ def _run(self, statement, parameters):
656653
)
657654
return result
658655

659-
def _begin(self):
656+
def _open_transaction(self, access_mode=None):
657+
self._transaction = Transaction(self, on_close=self._close_transaction)
658+
self._connect(access_mode)
660659
self._assert_open()
661660
metadata = {}
662661
parameters = {}
@@ -666,27 +665,7 @@ def _begin(self):
666665
cx = self._connection
667666
cx.run(u"BEGIN", parameters, metadata)
668667
cx.pull_all(metadata)
669-
670-
def _commit(self):
671-
self._assert_open()
672-
metadata = {}
673-
try:
674-
cx = self._connection
675-
cx.run(u"COMMIT", {}, metadata)
676-
cx.pull_all(metadata)
677-
finally:
678-
self._disconnect(sync=True)
679-
return metadata.get("bookmark")
680-
681-
def _rollback(self):
682-
self._assert_open()
683-
metadata = {}
684-
try:
685-
cx = self._connection
686-
cx.run(u"ROLLBACK", {}, metadata)
687-
cx.pull_all(metadata)
688-
finally:
689-
self._disconnect(sync=True)
668+
return self._transaction
690669

691670

692671
class Transaction(object):

0 commit comments

Comments
 (0)