Skip to content

Prevent pool from closing multiple times #188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
============
Expand Down
14 changes: 10 additions & 4 deletions neo4j/bolt/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,10 +507,16 @@ 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:
pass

def closed(self):
""" Return :const:`True` if this pool is closed, :const:`False`
Expand Down
20 changes: 7 additions & 13 deletions neo4j/v1/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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"])

Expand Down Expand Up @@ -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):
Expand Down