Check the connection status before putting back into the pool

Rollback connections in transaction or in error.
Discard broken connections.
Closes ticket #62.
This commit is contained in:
Daniele Varrazzo 2011-06-30 16:44:07 +01:00
parent 11ff27b5af
commit a2ee25ecfe
2 changed files with 18 additions and 1 deletions

2
NEWS
View File

@ -3,6 +3,8 @@ What's new in psycopg 2.4.3
- Fixed segfault in case of transaction started with connection lost
(and possibly other events).
- Rollback connections in transaction or in error before putting them
back into a pool. Also discard broken connections (ticket #62).
- Lazy import of the slow uuid module, thanks to Marko Kreen.

View File

@ -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
@ -121,7 +122,21 @@ class AbstractConnectionPool(object):
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()