Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use READ ONLY transaction mode in SQLAlchemy?

In PostgreSQL transaction access mode can be changed to READ ONLY (docs). In SQLAlchemy, isolation level can be changed for an engine, but no parameter is provided for read-only access mode (docs).

How to make an engine with READ ONLY access mode on connections?

like image 688
Taha Jahangir Avatar asked Sep 18 '14 04:09

Taha Jahangir


Video Answer


1 Answers

One solution is to execute a statement on every transaction:

engine = create_engine('postgresql+psycopg2://postgres:[email protected]:5432/')
@event.listens_for(engine, 'begin')
def receive_begin(conn):
    conn.execute('SET TRANSACTION READ ONLY')

But it's better to set mode in BEGIN TRANSACTION line an not a separate statement.

like image 166
Taha Jahangir Avatar answered Sep 30 '22 00:09

Taha Jahangir