rglsk/fastapi-sqlalchemy-1.4-async

Async GetDb Dependency does not work

Closed this issue · 4 comments

You can't issue a commit inside a dependency function after the path operation has completed because the results will already have been returned to the api caller. Any exceptions at this point cannot affect execution, but the transaction will have been rolled back.

https://github.com/rglsk/fastapi-sqlalchemy-1.4-async/blob/master/app/api/dependencies/db.py#L12

rglsk commented

Hey! Thanks for the issue, great catch! I have fixed it on my prod while ago but forgot about this implementation 👍

looks like this could solve the issue now
https://fastapi.tiangolo.com/release-notes/#0740

Maybe it does, I haven't tested it yet. How did you fix it in prod?

@rglsk which db are you using in production? As of now I'm getting around 1000 requests/second and even though I put max connections in postgresql to 1024 and increased the session poolsize (since it kept running out) it keeps making the database run out of connections

rglsk commented

Maybe it does, I haven't tested it yet. How did you fix it in prod?

@jeanlst 1st went with context manager skipping dependency, because as far as I remember I had also some issues with nested asyncio.gather.

Then as far as I remember I moved to this new way with catching errors like this.

which db are you using in production?
Mostly postgresql with replicas (something like this) or AWS Aurora with auto scaling