From df24bc7a52ed267fedc8e4e900cc932a09d6cbf7 Mon Sep 17 00:00:00 2001 From: Zhen Date: Tue, 10 Oct 2017 11:35:17 +0200 Subject: [PATCH 1/2] Prevent from closing driver multiple times Close driver object gracefully when it falls out of scope --- neo4j/bolt/connection.py | 15 +++++++++++---- neo4j/v1/api.py | 20 +++++++------------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/neo4j/bolt/connection.py b/neo4j/bolt/connection.py index 93a43964..05677bf1 100644 --- a/neo4j/bolt/connection.py +++ b/neo4j/bolt/connection.py @@ -507,10 +507,17 @@ def close(self): """ Close all connections and empty the pool. This method is thread safe. """ - with self.lock: - self._closed = True - for address in list(self.connections): - self.remove(address) + if self._closed: + return + try: + with self.lock: + if not self._closed: + self._closed = True + for address in list(self.connections): + self.remove(address) + except TypeError as e: + print(e) + pass def closed(self): """ Return :const:`True` if this pool is closed, :const:`False` diff --git a/neo4j/v1/api.py b/neo4j/v1/api.py index 3ea5278e..cb6c7049 100644 --- a/neo4j/v1/api.py +++ b/neo4j/v1/api.py @@ -41,6 +41,7 @@ RETRY_DELAY_MULTIPLIER = 2.0 RETRY_DELAY_JITTER_FACTOR = 0.2 + def last_bookmark(b0, b1): """ Return the latest of two bookmarks by looking for the maximum integer value following the last colon in the bookmark string. @@ -141,10 +142,7 @@ class Driver(object): #: Indicator of driver closure. _closed = False - _lock = None - def __init__(self, pool, **config): - self._lock = RLock() self._pool = pool self._max_retry_time = config.get("max_retry_time", default_config["max_retry_time"]) @@ -185,18 +183,14 @@ def close(self): """ Shut down, closing any open connections that were spawned by this Driver. """ - if self._lock is None: - return - with self._lock: - if not self._closed: - self._closed = True - if self._pool is not None: - self._pool.close() - self._pool = None + if not self._closed: + self._closed = True + if self._pool is not None: + self._pool.close() + self._pool = None def closed(self): - with self._lock: - return self._closed + return self._closed class Session(object): From 7ba85a9a61862ed63bb570c30fa961caa194061e Mon Sep 17 00:00:00 2001 From: Zhen Date: Tue, 10 Oct 2017 17:05:19 +0200 Subject: [PATCH 2/2] Added example of logging --- README.rst | 12 ++++++++++++ neo4j/bolt/connection.py | 1 - 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index 5d48c1bb..92fff20c 100644 --- a/README.rst +++ b/README.rst @@ -30,6 +30,18 @@ Quick Example session.write_transaction(add_friends, "Arthur", "Merlin") session.read_transaction(print_friends, "Arthur") +Logging +============= +The driver provides a built-in logging. The following example code enables debug logging and prints out logs at stdout: + +.. code-block:: python + + from neo4j.util import watch + import logging + from sys import stdout + + watch("neo4j.bolt", logging.DEBUG, stdout) + Installation ============ diff --git a/neo4j/bolt/connection.py b/neo4j/bolt/connection.py index 05677bf1..bef05105 100644 --- a/neo4j/bolt/connection.py +++ b/neo4j/bolt/connection.py @@ -516,7 +516,6 @@ def close(self): for address in list(self.connections): self.remove(address) except TypeError as e: - print(e) pass def closed(self):