From 94715ccbeceb94e6d08245ad234db4a653dbd677 Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 14 Feb 2013 14:52:27 +0100 Subject: [PATCH 1/2] [FIX] sql_db: free dead database connections lp bug: https://launchpad.net/bugs/905257 fixed bzr revid: chs@openerp.com-20130214135227-x6lzy3gj0s75ssmw --- openerp/sql_db.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/openerp/sql_db.py b/openerp/sql_db.py index 337964f3368..be15bdbdc6a 100644 --- a/openerp/sql_db.py +++ b/openerp/sql_db.py @@ -3,7 +3,7 @@ # # OpenERP, Open Source Management Solution # Copyright (C) 2004-2009 Tiny SPRL (). -# Copyright (C) 2010-2011 OpenERP s.a. (). +# Copyright (C) 2010-2013 OpenERP s.a. (). # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -398,8 +398,14 @@ class ConnectionPool(object): def borrow(self, dsn): self._debug('Borrow connection to %r', dsn) - # free leaked connections + # free dead and leaked connections for i, (cnx, _) in tools.reverse_enumerate(self._connections): + try: + cnx.reset() + except psycopg2.OperationalError: + self._debug('Cannot reset connection at index %d: %r', i, cnx.dsn) + cnx.close() + if cnx.closed: self._connections.pop(i) self._debug('Removing closed connection at index %d: %r', i, cnx.dsn) From e464e1231d593cb147571e2e59c337d4667054eb Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Fri, 15 Feb 2013 12:37:51 +0100 Subject: [PATCH 2/2] [FIX] sql_db: closing a closed connection is not allowed in psycopg2 2.4.4 and earlier Having the connections automatically reaped by psycopg2 is not guaranteed to happen all the time, so we still need to take extra steps to forece-close them bzr revid: odo@openerp.com-20130215113751-12kwmfynyt43qs57 --- openerp/sql_db.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/openerp/sql_db.py b/openerp/sql_db.py index be15bdbdc6a..a41f5e5077e 100644 --- a/openerp/sql_db.py +++ b/openerp/sql_db.py @@ -404,7 +404,9 @@ class ConnectionPool(object): cnx.reset() except psycopg2.OperationalError: self._debug('Cannot reset connection at index %d: %r', i, cnx.dsn) - cnx.close() + # psycopg2 2.4.4 and earlier do not allow closing a closed connection + if cnx.closed: + cnx.close() if cnx.closed: self._connections.pop(i)