Atomic transactions in a web framework #185
Unanswered
peterswords
asked this question in
Q&A
Replies: 1 comment 2 replies
-
It's a good question. I tend to manually wrap my view logic in the transaction context manager when required: @app.get("/")
async def my_endpoint():
try:
async with MyTable._meta.db.transaction():
# a bunch of queries
except Exception:
return JsonResponse({"message": "something went wrong"})
return JsonResponse({"message": "success"}) It would be nice to move the transaction handling out of the view though. A decorator is probably the best way to achieve this. Something like: @app.get("/")
@piccolo_transaction
async def my_endpoint():
... There isn't an out of the box solution for this yet, but I've created a ticket for it. |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
How would you wrap a fastapi function in a transaction and have it roll back if there are any errors during execution? I looked at transactions in the documentation, and see the "async with ..._meta.db.transaction()" approach. So I could write the "async with" at the top of every path function, but is there a way to make this happen generically (e.g. a fastapi dependency).
Beta Was this translation helpful? Give feedback.
All reactions