@@ -28,7 +28,7 @@ class which can be used to obtain `Driver` instances that are used for
28
28
29
29
from __future__ import division
30
30
31
- from collections import namedtuple
31
+ from collections import deque , namedtuple
32
32
33
33
from .compat import integer , perf_counter , string , urlparse
34
34
from .connection import connect , Response , RUN , PULL_ALL
@@ -91,15 +91,19 @@ def __init__(self, url, **config):
91
91
else :
92
92
raise ValueError ("Unsupported URL scheme: %s" % parsed .scheme )
93
93
self .config = config
94
+ self .sessions = deque ()
94
95
95
- def session (self , ** config ):
96
+ def session (self ):
96
97
""" Create a new session based on the graph database details
97
98
specified within this driver:
98
99
99
100
>>> session = driver.session()
100
101
101
102
"""
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 )
103
107
104
108
105
109
class Result (list ):
@@ -330,11 +334,15 @@ class Session(object):
330
334
method.
331
335
"""
332
336
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 )
335
340
self .transaction = None
336
341
self .bench_tests = []
337
342
343
+ def __del__ (self ):
344
+ self .connection .close ()
345
+
338
346
def __enter__ (self ):
339
347
return self
340
348
@@ -393,9 +401,9 @@ def run(self, statement, parameters=None):
393
401
return result
394
402
395
403
def close (self ):
396
- """ Shut down and close the session .
404
+ """ Return this session to the driver pool it came from .
397
405
"""
398
- self .connection . close ( )
406
+ self .driver . sessions . appendleft ( self )
399
407
400
408
def begin_transaction (self ):
401
409
""" Create a new :class:`.Transaction` within this session.
0 commit comments