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 |
I don't think this actually works in Flask-SQLAlchemy, I get the warning
SAWarning: Connection is already established for the given bind; execution_options ignored
because Flask-SQLAlchemy doesn't allow you to change the isolation level after the connection is established.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this thing would be any helpful?
I don't have any idea if this is going to work
but isn't going to make it easier? instead of adding this decorator before all routes?
This code is not tested, not sure if this even works