mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-22 17:06:33 +03:00
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:
parent
11ff27b5af
commit
a2ee25ecfe
2
NEWS
2
NEWS
|
@ -3,6 +3,8 @@ What's new in psycopg 2.4.3
|
||||||
|
|
||||||
- Fixed segfault in case of transaction started with connection lost
|
- Fixed segfault in case of transaction started with connection lost
|
||||||
(and possibly other events).
|
(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.
|
- Lazy import of the slow uuid module, thanks to Marko Kreen.
|
||||||
|
|
||||||
|
|
||||||
|
|
15
lib/pool.py
15
lib/pool.py
|
@ -25,6 +25,7 @@ This module implements thread-safe (and not) connection pools.
|
||||||
# License for more details.
|
# License for more details.
|
||||||
|
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
import psycopg2.extensions as _ext
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import logging
|
import logging
|
||||||
|
@ -121,7 +122,21 @@ class AbstractConnectionPool(object):
|
||||||
raise PoolError("trying to put unkeyed connection")
|
raise PoolError("trying to put unkeyed connection")
|
||||||
|
|
||||||
if len(self._pool) < self.minconn and not close:
|
if len(self._pool) < self.minconn and not close:
|
||||||
|
# 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)
|
self._pool.append(conn)
|
||||||
|
else:
|
||||||
|
# regular idle connection
|
||||||
|
self._pool.append(conn)
|
||||||
|
# If the connection is closed, we just discard it.
|
||||||
else:
|
else:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user