Skip to content

Commit e7ef51c

Browse files
Merge pull request #330 from martin-neotech/4.0-ConnectionExpired-removal
Removed ConnectionExpired
2 parents 809689d + 35deb5c commit e7ef51c

File tree

3 files changed

+57
-46
lines changed

3 files changed

+57
-46
lines changed

neo4j/exceptions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,3 +200,21 @@ class AuthError(ClientError, SecurityError):
200200
# DatabaseUnavailableError
201201
"Neo.TransientError.General.DatabaseUnavailable": DatabaseUnavailableError
202202
}
203+
204+
205+
class SessionExpired(Exception):
206+
""" Raised when no a session is no longer able to fulfil
207+
the purpose described by its original parameters.
208+
"""
209+
210+
def __init__(self, session, *args, **kwargs):
211+
super(SessionExpired, self).__init__(session, *args, **kwargs)
212+
213+
214+
class TransactionError(Exception):
215+
""" Raised when an error occurs while using a transaction.
216+
"""
217+
218+
def __init__(self, transaction, *args, **kwargs):
219+
super(TransactionError, self).__init__(*args, **kwargs)
220+
self.transaction = transaction

neo4j/io/__init__.py

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,24 @@
4949
from neo4j.conf import Config, PoolConfig
5050
from neo4j.io._bolt3 import Outbox, BufferedSocket, Inbox, Response, InitResponse, CommitResponse
5151
from neo4j.errors import BoltRoutingError, Neo4jAvailabilityError
52-
from neo4j.exceptions import ProtocolError, SecurityError, \
53-
ServiceUnavailable, AuthError, IncompleteCommitError, \
54-
ConnectionExpired, DatabaseUnavailableError, NotALeaderError, \
55-
ForbiddenOnReadOnlyDatabaseError, ClientError
52+
from neo4j.exceptions import (
53+
ProtocolError,
54+
SecurityError,
55+
ServiceUnavailable,
56+
AuthError,
57+
IncompleteCommitError,
58+
ConnectionExpired,
59+
DatabaseUnavailableError,
60+
NotALeaderError,
61+
ForbiddenOnReadOnlyDatabaseError,
62+
ClientError,
63+
SessionExpired,
64+
TransactionError,
65+
)
5666
from neo4j.meta import get_user_agent
5767
from neo4j.packstream import Packer, Unpacker
5868
from neo4j.routing import RoutingTable
5969

60-
6170
# Set up logger
6271
log = getLogger("neo4j")
6372

@@ -89,10 +98,6 @@ class Bolt:
8998
#: The pool of which this connection is a member
9099
pool = None
91100

92-
#: Error class used for raising connection errors
93-
# TODO: separate errors for connector API
94-
Error = ServiceUnavailable
95-
96101
@classmethod
97102
def ping(cls, address, *, timeout=None, **config):
98103
""" Attempt to establish a Bolt connection, returning the
@@ -305,13 +310,13 @@ def send_all(self):
305310
""" Send all queued messages to the server.
306311
"""
307312
if self.closed():
308-
raise self.Error("Failed to write to closed connection "
309-
"{!r} ({!r})".format(self.unresolved_address,
310-
self.server.address))
313+
raise ServiceUnavailable("Failed to write to closed connection {!r} ({!r})".format(
314+
self.unresolved_address, self.server.address))
315+
311316
if self.defunct():
312-
raise self.Error("Failed to write to defunct connection "
313-
"{!r} ({!r})".format(self.unresolved_address,
314-
self.server.address))
317+
raise ServiceUnavailable("Failed to write to defunct connection {!r} ({!r})".format(
318+
self.unresolved_address, self.server.address))
319+
315320
try:
316321
self._send_all()
317322
except (IOError, OSError) as error:
@@ -331,13 +336,13 @@ def fetch_message(self):
331336
messages fetched
332337
"""
333338
if self._closed:
334-
raise self.Error("Failed to read from closed connection "
335-
"{!r} ({!r})".format(self.unresolved_address,
336-
self.server.address))
339+
raise ServiceUnavailable("Failed to read from closed connection {!r} ({!r})".format(
340+
self.unresolved_address, self.server.address))
341+
337342
if self._defunct:
338-
raise self.Error("Failed to read from defunct connection "
339-
"{!r} ({!r})".format(self.unresolved_address,
340-
self.server.address))
343+
raise ServiceUnavailable("Failed to read from defunct connection {!r} ({!r})".format(
344+
self.unresolved_address, self.server.address))
345+
341346
if not self.responses:
342347
return 0, 0
343348

@@ -388,9 +393,11 @@ def fetch_message(self):
388393
return len(details), 1
389394

390395
def _set_defunct(self, error=None):
391-
message = ("Failed to read from defunct connection "
392-
"{!r} ({!r})".format(self.unresolved_address,
393-
self.server.address))
396+
direct_driver = isinstance(self.pool, BoltPool)
397+
398+
message = ("Failed to read from defunct connection {!r} ({!r})".format(
399+
self.unresolved_address, self.server.address))
400+
394401
log.error(message)
395402
# We were attempting to receive data but the connection
396403
# has unexpectedly terminated. So, we need to close the
@@ -406,7 +413,11 @@ def _set_defunct(self, error=None):
406413
for response in self.responses:
407414
if isinstance(response, CommitResponse):
408415
raise IncompleteCommitError(message)
409-
raise self.Error(message)
416+
417+
if direct_driver:
418+
raise ServiceUnavailable(message)
419+
else:
420+
raise SessionExpired(message)
410421

411422
def timedout(self):
412423
return 0 <= self._max_connection_lifetime <= perf_counter() - self._creation_timestamp
@@ -846,11 +857,9 @@ def acquire(self, access_mode=None, timeout=None):
846857
try:
847858
address = self._select_address(access_mode)
848859
except Neo4jAvailabilityError as err:
849-
raise ConnectionExpired("Failed to obtain connection "
850-
"towards '%s' server." % access_mode) from err
860+
raise SessionExpired("Failed to obtain connection towards '%s' server." % access_mode) from err
851861
try:
852862
connection = self._acquire(address, timeout=timeout) # should always be a resolved address
853-
connection.Error = ConnectionExpired
854863
except ServiceUnavailable:
855864
self.deactivate(address)
856865
else:

neo4j/work/simple.py

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
IncompleteCommitError,
3535
ServiceUnavailable,
3636
TransientError,
37+
SessionExpired,
38+
TransactionError,
3739
)
3840
from neo4j.work import Workspace, WorkspaceConfig
3941
from neo4j.work.summary import BoltStatementResultSummary
@@ -727,24 +729,6 @@ def data(self, *items):
727729
return [record.data(*items) for record in self.records()]
728730

729731

730-
class SessionExpired(Exception):
731-
""" Raised when no a session is no longer able to fulfil
732-
the purpose described by its original parameters.
733-
"""
734-
735-
def __init__(self, session, *args, **kwargs):
736-
super(SessionExpired, self).__init__(session, *args, **kwargs)
737-
738-
739-
class TransactionError(Exception):
740-
""" Raised when an error occurs while using a transaction.
741-
"""
742-
743-
def __init__(self, transaction, *args, **kwargs):
744-
super(TransactionError, self).__init__(*args, **kwargs)
745-
self.transaction = transaction
746-
747-
748732
def unit_of_work(metadata=None, timeout=None):
749733
""" This function is a decorator for transaction functions that allows
750734
extra control over how the transaction is carried out.

0 commit comments

Comments
 (0)