Last active
January 31, 2022 06:35
-
-
Save robyoung/68e2121c045f117bea78 to your computer and use it in GitHub Desktop.
Flask-SQLAlchemy set per request isolation level
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def isolation_level(level): | |
"""Return a Flask view decorator to set SQLAlchemy isolation level | |
Usage:: | |
@main.route("/thingy/<id>", methods=["POST"]) | |
@isolation_level("SERIALIZABLE") | |
def update_a_thing(id): | |
... | |
""" | |
def decorator(view): | |
def view_wrapper(*args, **kwargs): | |
db.session.connection(execution_options={'isolation_level': level}) | |
return view(*args, **kwargs) | |
return view_wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I don't think this actually works in Flask-SQLAlchemy, I get the warning
because Flask-SQLAlchemy doesn't allow you to change the isolation level after the connection is established.