4949
5050try :
5151 from neobolt .exceptions import (
52+ ConnectionExpired ,
5253 CypherError ,
53- TransientError ,
54+ IncompleteCommitError ,
5455 ServiceUnavailable ,
56+ TransientError ,
5557 )
5658except ImportError :
5759 # We allow this to fail because this module can be imported implicitly
5860 # during setup. At that point, dependencies aren't available.
5961 pass
6062else :
6163 __all__ .extend ([
64+ "ConnectionExpired" ,
6265 "CypherError" ,
63- "TransientError " ,
66+ "IncompleteCommitError " ,
6467 "ServiceUnavailable" ,
68+ "TransientError" ,
6569 ])
6670
6771
@@ -363,35 +367,31 @@ def _connect(self, access_mode=None):
363367 if access_mode is None :
364368 access_mode = self ._default_access_mode
365369 if self ._connection :
366- self ._disconnect (sync = True )
370+ self ._connection .sync ()
371+ self ._disconnect ()
367372 self ._connection = self ._acquirer (access_mode )
368373
369- def _disconnect (self , sync ):
370- from neobolt .exceptions import ConnectionExpired , ServiceUnavailable
374+ def _disconnect (self ):
371375 if self ._connection :
372- if sync :
373- try :
374- self ._connection .sync ()
375- except (SessionError , ConnectionExpired , ServiceUnavailable ):
376- pass
377- if self ._connection :
378- self ._connection .in_use = False
379- self ._connection = None
376+ self ._connection .in_use = False
377+ self ._connection = None
380378
381379 def close (self ):
382380 """ Close the session. This will release any borrowed resources,
383381 such as connections, and will roll back any outstanding transactions.
384382 """
385- from neobolt .exceptions import ConnectionExpired , CypherError , ServiceUnavailable
386- try :
387- if self .has_transaction ():
388- try :
389- self .rollback_transaction ()
390- except (CypherError , TransactionError , SessionError , ConnectionExpired , ServiceUnavailable ):
391- pass
392- finally :
393- self ._closed = True
394- self ._disconnect (sync = True )
383+ if self ._connection :
384+ if self ._transaction :
385+ self ._connection .rollback ()
386+ self ._transaction = None
387+ try :
388+ self ._connection .sync ()
389+ except (ConnectionExpired , CypherError , TransactionError ,
390+ ServiceUnavailable , SessionError ):
391+ pass
392+ finally :
393+ self ._disconnect ()
394+ self ._closed = True
395395
396396 def closed (self ):
397397 """ Indicator for whether or not this session has been closed.
@@ -554,7 +554,7 @@ def detach(self, result, sync=True):
554554 if self ._last_result is result :
555555 self ._last_result = None
556556 if not self .has_transaction ():
557- self ._disconnect (sync = False )
557+ self ._disconnect ()
558558
559559 result ._session = None
560560 return count
@@ -620,8 +620,11 @@ def commit_transaction(self):
620620 metadata = {}
621621 try :
622622 self ._connection .commit (on_success = metadata .update )
623+ self ._connection .sync ()
624+ except IncompleteCommitError :
625+ raise ServiceUnavailable ("Connection closed during commit" )
623626 finally :
624- self ._disconnect (sync = True )
627+ self ._disconnect ()
625628 self ._transaction = None
626629 bookmark = metadata .get ("bookmark" )
627630 self ._bookmarks_in = tuple ([bookmark ])
@@ -641,8 +644,9 @@ def rollback_transaction(self):
641644 metadata = {}
642645 try :
643646 cx .rollback (on_success = metadata .update )
647+ cx .sync ()
644648 finally :
645- self ._disconnect (sync = True )
649+ self ._disconnect ()
646650 self ._transaction = None
647651
648652 def _run_transaction (self , access_mode , unit_of_work , * args , ** kwargs ):
0 commit comments