diff --git a/trytond/trytond/backend/postgresql/database.py b/trytond/trytond/backend/postgresql/database.py index 5cb21347d4..0a6f2ae74e 100644 --- a/trytond/trytond/backend/postgresql/database.py +++ b/trytond/trytond/backend/postgresql/database.py @@ -24,6 +24,7 @@ except ImportError: PYDATE, PYDATETIME, PYTIME, PYINTERVAL = None, None, None, None from psycopg2 import DataError as DatabaseDataError from psycopg2 import IntegrityError as DatabaseIntegrityError +from psycopg2 import InterfaceError from psycopg2 import OperationalError as DatabaseOperationalError from psycopg2 import ProgrammingError from psycopg2.errors import QueryCanceled as DatabaseTimeoutError @@ -291,7 +292,10 @@ class Database(DatabaseInterface): return conn def put_connection(self, connection, close=False): - connection.reset() + try: + connection.reset() + except InterfaceError: + pass self._connpool.putconn(connection, close=close) def close(self): diff --git a/trytond/trytond/transaction.py b/trytond/trytond/transaction.py index bc0c2b836d..b078a4dff4 100644 --- a/trytond/trytond/transaction.py +++ b/trytond/trytond/transaction.py @@ -238,8 +238,9 @@ class Transaction(object): else: self.rollback() finally: - self.database.put_connection( - self.connection, self.close) + if self.connection: + self.database.put_connection( + self.connection, self.close) finally: self.database = None self.readonly = False @@ -357,7 +358,8 @@ class Transaction(object): datamanager.tpc_abort(self) Cache.rollback(self) self._clear_log_records() - self.connection.rollback() + if self.connection: + self.connection.rollback() def join(self, datamanager): try: diff --git a/trytond/trytond/wsgi.py b/trytond/trytond/wsgi.py index 2c6f69b2ad..94f430261c 100644 --- a/trytond/trytond/wsgi.py +++ b/trytond/trytond/wsgi.py @@ -26,6 +26,7 @@ try: except ImportError: from werkzeug.wsgi import SharedDataMiddleware +from trytond import backend from trytond.config import config from trytond.protocols.jsonrpc import JSONProtocol from trytond.protocols.wrappers import ( @@ -111,6 +112,10 @@ class TrytondWSGI(object): logger.debug( "Exception when processing %s", request, exc_info=True) return e + except backend.DatabaseOperationalError as e: + logger.debug( + "Exception when processing %s", request, exc_info=True) + return exceptions.ServiceUnavailable(description=str(e)) except Exception as e: logger.debug( "Exception when processing %s", request, exc_info=True)