Remix.run Logo
mrkaye97 12 hours ago

+1 to this - I've griped pretty often that FastAPI's documentation implicitly recommends this (https://fastapi.tiangolo.com/tutorial/sql-databases/#create-...) by suggesting using dependency injection to manage database connections, only to start seeing connection pool exhausted errors as soon as the number of concurrent requests exceeds the number of allowed connections.

frollogaston 12 hours ago | parent [-]

Oh wow. Dep injection for DB connections is nasty.

alfons_foobar 11 hours ago | parent [-]

I might be outing myself as a noob here, but... what is the (better) alternative?

0x696C6961 11 hours ago | parent [-]

You inject the pool itself.

alfons_foobar 39 minutes ago | parent [-]

Sorry, I am being dense... how does that solve the problem?

I still have to get a connection from the pool, I just do it inside the function body now, right?

So this

    @app.get("/users")
    def get_users(conn = Depends[get_db_conn]):
        users = conn.execute("SELECT * FROM users")
        return users
would become that instead:

    @app.get("/users")
    def get_users(pool = Depends[get_db_pool]):
        with pool.get_conn() as conn:
            users = conn.execute("SELECT * FROM users")
        return users
But I still need enough connections in the pool to handle all concurrent requests, no?