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
This commit is contained in:
Cédric Krier committed 2020-07-21 14:29:14 +02:00
1 parent 46995607e2
commit f376687b5a
1 file changed
+7 -1
+7 -1
View File
@@ -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