@@ -229,6 +229,7 @@ def __init__(self, creator,
229229 raise NotSupportedError ("Database module is not thread-safe." )
230230 self ._creator = creator
231231 self ._args , self ._kwargs = args , kwargs
232+ self ._blocking = blocking
232233 self ._maxusage = maxusage
233234 self ._setsession = setsession
234235 self ._reset = reset
@@ -260,11 +261,7 @@ def __init__(self, creator,
260261 else :
261262 self ._maxconnections = 0
262263 self ._idle_cache = [] # the actual pool of idle connections
263- self ._condition = Condition ()
264- if not blocking :
265- def wait ():
266- raise TooManyConnections
267- self ._condition .wait = wait
264+ self ._lock = Condition ()
268265 self ._connections = 0
269266 # Establish an initial number of idle database connections:
270267 idle = [self .dedicated_connection () for i in range (mincached )]
@@ -286,11 +283,11 @@ def connection(self, shareable=True):
286283
287284 """
288285 if shareable and self ._maxshared :
289- self ._condition .acquire ()
286+ self ._lock .acquire ()
290287 try :
291288 while (not self ._shared_cache and self ._maxconnections
292289 and self ._connections >= self ._maxconnections ):
293- self ._condition . wait ()
290+ self ._wait_lock ()
294291 if len (self ._shared_cache ) < self ._maxshared :
295292 # shared cache is not full, get a dedicated connection
296293 try : # first try to get it from the idle cache
@@ -307,23 +304,23 @@ def connection(self, shareable=True):
307304 while con .con ._transaction :
308305 # do not share connections which are in a transaction
309306 self ._shared_cache .insert (0 , con )
310- self ._condition . wait ()
307+ self ._wait_lock ()
311308 self ._shared_cache .sort ()
312309 con = self ._shared_cache .pop (0 )
313310 con .con ._ping_check () # check the underlying connection
314311 con .share () # increase share of this connection
315312 # put the connection (back) into the shared cache
316313 self ._shared_cache .append (con )
317- self ._condition .notify ()
314+ self ._lock .notify ()
318315 finally :
319- self ._condition .release ()
316+ self ._lock .release ()
320317 con = PooledSharedDBConnection (self , con )
321318 else : # try to get a dedicated connection
322- self ._condition .acquire ()
319+ self ._lock .acquire ()
323320 try :
324321 while (self ._maxconnections
325322 and self ._connections >= self ._maxconnections ):
326- self ._condition . wait ()
323+ self ._wait_lock ()
327324 # connection limit not reached, get a dedicated connection
328325 try : # first try to get it from the idle cache
329326 con = self ._idle_cache .pop (0 )
@@ -334,7 +331,7 @@ def connection(self, shareable=True):
334331 con = PooledDedicatedDBConnection (self , con )
335332 self ._connections += 1
336333 finally :
337- self ._condition .release ()
334+ self ._lock .release ()
338335 return con
339336
340337 def dedicated_connection (self ):
@@ -343,7 +340,7 @@ def dedicated_connection(self):
343340
344341 def unshare (self , con ):
345342 """Decrease the share of a connection in the shared cache."""
346- self ._condition .acquire ()
343+ self ._lock .acquire ()
347344 try :
348345 con .unshare ()
349346 shared = con .shared
@@ -353,13 +350,13 @@ def unshare(self, con):
353350 except ValueError :
354351 pass # pool has already been closed
355352 finally :
356- self ._condition .release ()
353+ self ._lock .release ()
357354 if not shared : # connection has become idle,
358355 self .cache (con .con ) # so add it to the idle cache
359356
360357 def cache (self , con ):
361358 """Put a dedicated connection back into the idle cache."""
362- self ._condition .acquire ()
359+ self ._lock .acquire ()
363360 try :
364361 if not self ._maxcached or len (self ._idle_cache ) < self ._maxcached :
365362 con ._reset (force = self ._reset ) # rollback possible transaction
@@ -368,13 +365,13 @@ def cache(self, con):
368365 else : # if the idle cache is already full,
369366 con .close () # then close the connection
370367 self ._connections -= 1
371- self ._condition .notify ()
368+ self ._lock .notify ()
372369 finally :
373- self ._condition .release ()
370+ self ._lock .release ()
374371
375372 def close (self ):
376373 """Close all connections in the pool."""
377- self ._condition .acquire ()
374+ self ._lock .acquire ()
378375 try :
379376 while self ._idle_cache : # close all idle connections
380377 con = self ._idle_cache .pop (0 )
@@ -390,9 +387,9 @@ def close(self):
390387 except Exception :
391388 pass
392389 self ._connections -= 1
393- self ._condition .notifyAll ()
390+ self ._lock .notifyAll ()
394391 finally :
395- self ._condition .release ()
392+ self ._lock .release ()
396393
397394 def __del__ (self ):
398395 """Delete the pool."""
@@ -401,6 +398,12 @@ def __del__(self):
401398 except Exception :
402399 pass
403400
401+ def _wait_lock (self ):
402+ """Wait until notified or report an error."""
403+ if not self ._blocking :
404+ raise TooManyConnections
405+ self ._lock .wait ()
406+
404407
405408# Auxiliary classes for pooled connections
406409
0 commit comments