@@ -28,7 +28,7 @@ class which can be used to obtain `Driver` instances that are used for
2828
2929from  __future__ import  division 
3030
31- from  collections  import  namedtuple 
31+ from  collections  import  deque ,  namedtuple 
3232
3333from  .compat  import  integer , perf_counter , string , urlparse 
3434from  .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
105109class  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. 
0 commit comments