mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-02-07 12:50:32 +03:00
piro pool.py patch.
This commit is contained in:
parent
feeea0309a
commit
b7e5e5e272
10
ChangeLog
10
ChangeLog
|
@ -1,9 +1,15 @@
|
|||
2005-09-23 Federico Di Gregorio <fog@debian.org>
|
||||
|
||||
* lib/pool.py: applied patch from piro to avoid the scan of the
|
||||
whole connection array on getconn().
|
||||
|
||||
2005-09-12 Federico Di Gregorio <fog@initd.org>
|
||||
|
||||
* lib/pool.py: Applied psycopg->psycopg2 patch to from bug #35.
|
||||
|
||||
* ZpsycopgDA/db.py: fixed problem with OperationalError that resulted in
|
||||
cryptic message to Zope users ("'OperationalError' is not defined".)
|
||||
* ZpsycopgDA/db.py: fixed problem with OperationalError that
|
||||
resulted in cryptic message to Zope users ("'OperationalError' is
|
||||
not defined".)
|
||||
|
||||
2005-08-23 Federico Di Gregorio <fog@debian.org>
|
||||
|
||||
|
|
24
lib/pool.py
24
lib/pool.py
|
@ -53,6 +53,7 @@ class AbstractConnectionPool(object):
|
|||
|
||||
self._pool = []
|
||||
self._used = {}
|
||||
self._rused = {} # id(conn) -> key map
|
||||
self._keys = 0
|
||||
|
||||
for i in range(self.minconn):
|
||||
|
@ -63,6 +64,7 @@ class AbstractConnectionPool(object):
|
|||
conn = psycopg2.connect(*self._args, **self._kwargs)
|
||||
if key is not None:
|
||||
self._used[key] = conn
|
||||
self._rused[id(conn)] = key
|
||||
else:
|
||||
self._pool.append(conn)
|
||||
return conn
|
||||
|
@ -72,30 +74,27 @@ class AbstractConnectionPool(object):
|
|||
self._keys += 1
|
||||
return self._keys
|
||||
|
||||
def _findkey(self, conn):
|
||||
"""Return the key associated with a connection or None."""
|
||||
for o, k in self._used.items():
|
||||
if o == conn:
|
||||
return k
|
||||
|
||||
def _getconn(self, key=None):
|
||||
"""Get a free connection and assign it to 'key' if not None."""
|
||||
if self.closed: raise PoolError("connection pool is closed")
|
||||
if key is None: key = self._getkey()
|
||||
|
||||
if not self._used.has_key(key):
|
||||
if not self._pool:
|
||||
if self._used.has_key(key):
|
||||
return self._used[key]
|
||||
|
||||
if self._pool:
|
||||
self._used[key] = conn = self._pool.pop()
|
||||
self._rused[id(conn)] = key
|
||||
return conn
|
||||
else:
|
||||
if len(self._used) == self.maxconn:
|
||||
raise PoolError("connection pool exausted")
|
||||
return self._connect(key)
|
||||
else:
|
||||
self._used[key] = self._pool.pop()
|
||||
return self._used[key]
|
||||
|
||||
def _putconn(self, conn, key=None, close=False):
|
||||
"""Put away a connection."""
|
||||
if self.closed: raise PoolError("connection pool is closed")
|
||||
if key is None: key = self._findkey(conn)
|
||||
if key is None: key = self._rused[id(conn)]
|
||||
|
||||
if not key:
|
||||
raise PoolError("trying to put unkeyed connection")
|
||||
|
@ -109,6 +108,7 @@ class AbstractConnectionPool(object):
|
|||
# thread tries to put back a connection after a call to close
|
||||
if not self.closed or key in self._used:
|
||||
del self._used[key]
|
||||
del self._rused[id(conn)]
|
||||
|
||||
def _closeall(self):
|
||||
"""Close all connections.
|
||||
|
|
Loading…
Reference in New Issue
Block a user