Skip to content

Commit 90397b4

Browse files
committed
Session pool
1 parent 18e0d61 commit 90397b4

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

neo4j/v1/session.py

+15-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class which can be used to obtain `Driver` instances that are used for
2828

2929
from __future__ import division
3030

31-
from collections import namedtuple
31+
from collections import deque, namedtuple
3232

3333
from .compat import integer, perf_counter, string, urlparse
3434
from .connection import connect, Response, RUN, PULL_ALL
@@ -91,15 +91,19 @@ def __init__(self, url, **config):
9191
else:
9292
raise ValueError("Unsupported URL scheme: %s" % parsed.scheme)
9393
self.config = config
94+
self.sessions = deque()
9495

95-
def session(self, **config):
96+
def session(self):
9697
""" Create a new session based on the graph database details
9798
specified within this driver:
9899
99100
>>> session = driver.session()
100101
101102
"""
102-
return Session(connect(self.host, self.port, **dict(self.config, **config)))
103+
try:
104+
return self.sessions.pop()
105+
except IndexError:
106+
return Session(self)
103107

104108

105109
class Result(list):
@@ -330,11 +334,15 @@ class Session(object):
330334
method.
331335
"""
332336

333-
def __init__(self, connection):
334-
self.connection = connection
337+
def __init__(self, driver):
338+
self.driver = driver
339+
self.connection = connect(driver.host, driver.port, **driver.config)
335340
self.transaction = None
336341
self.bench_tests = []
337342

343+
def __del__(self):
344+
self.connection.close()
345+
338346
def __enter__(self):
339347
return self
340348

@@ -393,9 +401,9 @@ def run(self, statement, parameters=None):
393401
return result
394402

395403
def close(self):
396-
""" Shut down and close the session.
404+
""" Return this session to the driver pool it came from.
397405
"""
398-
self.connection.close()
406+
self.driver.sessions.appendleft(self)
399407

400408
def begin_transaction(self):
401409
""" Create a new :class:`.Transaction` within this session.

test/test_session.py

+16
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,22 @@ def test_must_use_valid_url_scheme(self):
3030
with self.assertRaises(ValueError):
3131
GraphDatabase.driver("x://xxx")
3232

33+
def test_sessions_are_reused(self):
34+
driver = GraphDatabase.driver("bolt://localhost")
35+
session_1 = driver.session()
36+
session_1.close()
37+
session_2 = driver.session()
38+
session_2.close()
39+
assert session_1 is session_2
40+
41+
def test_sessions_are_not_reused_if_still_in_use(self):
42+
driver = GraphDatabase.driver("bolt://localhost")
43+
session_1 = driver.session()
44+
session_2 = driver.session()
45+
session_2.close()
46+
session_1.close()
47+
assert session_1 is not session_2
48+
3349
def test_can_run_simple_statement(self):
3450
session = GraphDatabase.driver("bolt://localhost").session()
3551
count = 0

0 commit comments

Comments
 (0)