flask-sqlalchemy icon indicating copy to clipboard operation
flask-sqlalchemy copied to clipboard

Set isolation level for individual transactions (or individual sessions)

Open k-time opened this issue 3 years ago • 0 comments

In SQLAlchemy, the isolation level can be set for an individual transaction: https://docs.sqlalchemy.org/en/14/orm/session_transaction.html#setting-isolation-for-individual-transactions

However, it states that

A key caveat regarding isolation level is that the setting cannot be safely modified on a Connection where a transaction has already started.

so when I try to change the isolation level for a transaction as follows: db.session.connection(execution_options={"isolation_level": "REPEATABLE READ"}) I get the following warning:

.../venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py:414: SAWarning: Connection is already established for the given bind; execution_options ignored

The only workaround I've found is to close the session first using db.session.close(), and then changing the isolation level using the above code. However this is probably suboptimal because a new session is started, closed, and then started again every time my endpoint is hit?

This would be a nice feature to have—thanks for any help!

k-time avatar Jan 31 '22 06:01 k-time