From f376687b5a3cd19bcd396f1b91a993457780a2a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Tue, 21 Jul 2020 14:29:14 +0200 Subject: [PATCH] Use readonly connection attribute It is not allowed to Call SET TRANSACTION in an AUTOCOMMIT transaction. It is better to set 'readonly' attribute when available and not set the transaction in readonly when autocommit is set. issue9477 review294171002 --- trytond/backend/postgresql/database.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/trytond/backend/postgresql/database.py b/trytond/backend/postgresql/database.py index 98e25818da..0ab83431d8 100644 --- a/trytond/backend/postgresql/database.py +++ b/trytond/backend/postgresql/database.py @@ -221,11 +221,17 @@ class Database(DatabaseInterface): logger.error( 'connection to "%s" failed', self.name, exc_info=True) raise + # We do not use set_session because psycopg2 < 2.7 and psycopg2cffi + # change the default_transaction_* attributes which breaks external + # pooling at the transaction level. if autocommit: conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT) else: conn.set_isolation_level(ISOLATION_LEVEL_REPEATABLE_READ) - if readonly: + # psycopg2cffi does not have the readonly property + if hasattr(conn, 'readonly'): + conn.readonly = readonly + elif not autocommit: cursor = conn.cursor() cursor.execute('SET TRANSACTION READ ONLY') return conn