Skip to content

Commit 37fdc34

Browse files
committed
Removed driver.close(); fixed session.close()
1 parent 8240bd5 commit 37fdc34

File tree

2 files changed

+13
-19
lines changed

2 files changed

+13
-19
lines changed

examples/test_examples.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class FreshDatabaseTestCase(TestCase):
3030

3131
def setUp(self):
3232
session = GraphDatabase.driver("bolt://localhost").session()
33-
session.run("MATCH (n) DETACH DELETE n").close()
33+
session.run("MATCH (n) DETACH DELETE n")
3434
session.close()
3535

3636

@@ -48,7 +48,6 @@ def test_minimal_working_example(self):
4848
print("Neo is %d years old." % cursor["p.age"])
4949

5050
session.close()
51-
driver.close()
5251
# end::minimal-example[]
5352

5453

@@ -91,7 +90,6 @@ def test_statement(self):
9190
session.run("CREATE (person:Person {name: {name}})", {"name": "Neo"}).close()
9291
# end::statement[]
9392
session.close()
94-
driver.close()
9593

9694
def test_statement_without_parameters(self):
9795
driver = GraphDatabase.driver("bolt://localhost")
@@ -100,7 +98,6 @@ def test_statement_without_parameters(self):
10098
session.run("CREATE (person:Person {name: 'Neo'})").close()
10199
# end::statement-without-parameters[]
102100
session.close()
103-
driver.close()
104101

105102
def test_result_cursor(self):
106103
driver = GraphDatabase.driver("bolt://localhost")
@@ -114,7 +111,6 @@ def test_result_cursor(self):
114111
print(cursor["tool.name"])
115112
# end::result-cursor[]
116113
session.close()
117-
driver.close()
118114

119115
def test_cursor_nesting(self):
120116
driver = GraphDatabase.driver("bolt://localhost")
@@ -128,7 +124,6 @@ def test_cursor_nesting(self):
128124
"CREATE (person)-[:REPORTS_TO]->(boss)", {"id": cursor["minion"], "boss": "Bob"})
129125
# end::retain-result-query[]
130126
session.close()
131-
driver.close()
132127

133128
def test_result_retention(self):
134129
driver = GraphDatabase.driver("bolt://localhost")
@@ -144,7 +139,6 @@ def test_result_retention(self):
144139
"CREATE (person)-[:REPORTS_TO]->(boss)", {"id": record["minion"], "boss": "Bob"})
145140
# end::retain-result-process[]
146141
session.close()
147-
driver.close()
148142

149143
def test_transaction_commit(self):
150144
driver = GraphDatabase.driver("bolt://localhost")
@@ -159,7 +153,6 @@ def test_transaction_commit(self):
159153
assert cursor["count(p)"] == 1
160154
assert cursor.at_end()
161155
session.close()
162-
driver.close()
163156

164157
def test_transaction_rollback(self):
165158
driver = GraphDatabase.driver("bolt://localhost")
@@ -174,7 +167,6 @@ def test_transaction_rollback(self):
174167
assert cursor["count(p)"] == 0
175168
assert cursor.at_end()
176169
session.close()
177-
driver.close()
178170

179171
def test_result_summary_query_profile(self):
180172
driver = GraphDatabase.driver("bolt://localhost")
@@ -187,7 +179,6 @@ def test_result_summary_query_profile(self):
187179
print(summary.profile)
188180
# end::result-summary-query-profile[]
189181
session.close()
190-
driver.close()
191182

192183
def test_result_summary_notifications(self):
193184
driver = GraphDatabase.driver("bolt://localhost")
@@ -198,4 +189,3 @@ def test_result_summary_notifications(self):
198189
print(notification)
199190
# end::result-summary-notifications[]
200191
session.close()
201-
driver.close()

neo4j/v1/session.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def session(self):
102102
return session
103103

104104
def recycle(self, session):
105-
""" Pass a session back to the driver for recycling, if healthy.
105+
""" Accept a session for recycling, if healthy.
106106
107107
:param session:
108108
:return:
@@ -114,9 +114,6 @@ def recycle(self, session):
114114
if session.healthy and len(pool) < self.max_pool_size and session not in pool:
115115
pool.appendleft(session)
116116

117-
def close(self):
118-
pass # TODO
119-
120117

121118
class ResultCursor(object):
122119
""" A handler for the result of Cypher statement execution.
@@ -138,6 +135,7 @@ def __init__(self, connection, statement, parameters):
138135
self._next = deque()
139136
self._position = -1
140137
self._summary = None
138+
self._consumed = False
141139

142140
def is_open(self):
143141
""" Return ``True`` if this cursor is still open, ``False`` otherwise.
@@ -161,7 +159,7 @@ def next(self):
161159
self._current = Record(self.keys, tuple(map(hydrated, values)))
162160
self._position += 1
163161
return True
164-
elif self._summary:
162+
elif self._consumed:
165163
return False
166164
else:
167165
self._connection.fetch_next()
@@ -183,7 +181,7 @@ def at_end(self):
183181
"""
184182
if self._next:
185183
return False
186-
elif self._summary:
184+
elif self._consumed:
187185
return True
188186
else:
189187
self._connection.fetch_next()
@@ -221,7 +219,7 @@ def summarize(self):
221219
def _consume(self):
222220
# Consume the remainder of this result, triggering all appropriate callback functions.
223221
fetch_next = self._connection.fetch_next
224-
while self._summary is None:
222+
while not self._consumed:
225223
fetch_next()
226224

227225
def _on_header(self, metadata):
@@ -235,9 +233,11 @@ def _on_record(self, values):
235233
def _on_footer(self, metadata):
236234
# Called on receipt of the result footer.
237235
self._summary = ResultSummary(self.statement, self.parameters, **metadata)
236+
self._consumed = True
238237

239238
def _on_failure(self, metadata):
240239
# Called on execution failure.
240+
self._consumed = True
241241
raise CypherError(metadata)
242242

243243

@@ -411,6 +411,7 @@ def __init__(self, driver):
411411
self.driver = driver
412412
self.connection = connect(driver.host, driver.port, **driver.config)
413413
self.transaction = None
414+
self.last_cursor = None
414415

415416
def __del__(self):
416417
if not self.connection.closed:
@@ -468,11 +469,14 @@ def run(self, statement, parameters=None):
468469
self.connection.append(PULL_ALL, response=pull_all_response)
469470
self.connection.send()
470471

472+
self.last_cursor = cursor
471473
return cursor
472474

473475
def close(self):
474-
""" If still usable, return this session to the driver pool it came from.
476+
""" Recycle this session through the driver it came from.
475477
"""
478+
if self.last_cursor:
479+
self.last_cursor.close()
476480
self.driver.recycle(self)
477481

478482
def begin_transaction(self):

0 commit comments

Comments
 (0)