From 1f9a834c9bb2b2dc06da8e6e9b8cae7a67e68ce6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 10 May 2016 10:35:23 +0200 Subject: [PATCH] Wait for a connection in PostgreSQL backend If the pool is exhausted, we try to get one during few seconds before raising an exception. issue5435 review22911002 --- trytond/backend/postgresql/database.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/trytond/backend/postgresql/database.py b/trytond/backend/postgresql/database.py index 505973a878..e0c11f4306 100644 --- a/trytond/backend/postgresql/database.py +++ b/trytond/backend/postgresql/database.py @@ -13,7 +13,7 @@ try: except ImportError: pass from psycopg2 import connect -from psycopg2.pool import ThreadedConnectionPool +from psycopg2.pool import ThreadedConnectionPool, PoolError from psycopg2.extensions import ISOLATION_LEVEL_REPEATABLE_READ from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT from psycopg2.extensions import register_type, register_adapter @@ -96,7 +96,16 @@ class Database(DatabaseInterface): def get_connection(self, autocommit=False, readonly=False): if self._connpool is None: self.connect() - conn = self._connpool.getconn() + for count in range(config.getint('database', 'retry'), -1, -1): + try: + conn = self._connpool.getconn() + break + except PoolError: + if count and not self._connpool.closed: + logger.info('waiting a connection') + time.sleep(1) + continue + raise if autocommit: conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) else: