diff options
author | Federico Di Gregorio <fog@initd.org> | 2011-08-10 18:36:24 +0200 |
---|---|---|
committer | Federico Di Gregorio <fog@initd.org> | 2011-08-10 18:36:24 +0200 |
commit | a59d88c703fb254f5f0c1e57f55d28fd8690a648 (patch) | |
tree | b054f4f9a54cc13118f86ffe2369b62cd9b95430 /lib | |
parent | 479bdf7458fcf766c3e5d66c07772235c73c36eb (diff) | |
parent | 1861e0010d2a40c32a9793c5358304786f2b0d1d (diff) | |
download | psycopg2-a59d88c703fb254f5f0c1e57f55d28fd8690a648.tar.gz |
Merge remote-tracking branch 'piro/devel' into devel
Diffstat (limited to 'lib')
-rw-r--r-- | lib/extras.py | 2 | ||||
-rw-r--r-- | lib/pool.py | 19 |
2 files changed, 18 insertions, 3 deletions
diff --git a/lib/extras.py b/lib/extras.py index a36faa8..0d7bc98 100644 --- a/lib/extras.py +++ b/lib/extras.py @@ -275,7 +275,7 @@ class NamedTupleCursor(_cursor): def executemany(self, query, vars): self.Record = None - return _cursor.executemany(self, vars) + return _cursor.executemany(self, query, vars) def callproc(self, procname, vars=None): self.Record = None diff --git a/lib/pool.py b/lib/pool.py index 8a8fa53..da8d305 100644 --- a/lib/pool.py +++ b/lib/pool.py @@ -25,6 +25,7 @@ This module implements thread-safe (and not) connection pools. # License for more details. import psycopg2 +import psycopg2.extensions as _ext try: import logging @@ -115,13 +116,27 @@ class AbstractConnectionPool(object): 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._rused[id(conn)] + if key is None: key = self._rused.get(id(conn)) if not key: raise PoolError("trying to put unkeyed connection") if len(self._pool) < self.minconn and not close: - self._pool.append(conn) + # Return the connection into a consistent state before putting + # it back into the pool + if not conn.closed: + status = conn.get_transaction_status() + if status == _ext.TRANSACTION_STATUS_UNKNOWN: + # server connection lost + conn.close() + elif status != _ext.TRANSACTION_STATUS_IDLE: + # connection in error or in transaction + conn.rollback() + self._pool.append(conn) + else: + # regular idle connection + self._pool.append(conn) + # If the connection is closed, we just discard it. else: conn.close() |