Open agusmdev opened 11 months ago
@agusmdev you dont want a session/connection per request right? I thought that what you want is that connections are reused
@agusmdev wouldnt a possible solution be to create the session before a request, with middleware, and close it afterrwards
@agusmdev you dont want a session/connection per request right? I thought that what you want is that connections are reused
@theobouwman Mmm I think that I do want 1 session per request. I'm using FastAPI and I'd like to emulate the same injection that you can do with FastAPI built-in Depends
but given that I want to inject the session in a service and not in a router I can't use the FastAPI built-in injector.
@agusmdev wouldnt a possible solution be to create the session before a request, with middleware, and close it afterrwards
Yes, it's possible, but the session per request is working correctly with the implementation I have, the issue appears when you have a bunch of requests at the same time, the sessions get reused by the requests because the implementation of Resource
is like a Singleton
and not like a Factory
(or that's what I guess that's happening here)
@agusmdev okay I get it. I have the issue that with this implementation (https://github.com/ets-labs/python-dependency-injector/blob/master/examples/miniapps/fastapi-sqlalchemy/webapp/database.py) each query a new session is created, which is not what we want because this takes time as you can see in the screenshot.
Yes, it's possible, but the session per request is working correctly with the implementation I have, the issue appears when you have a bunch of requests at the same time, the sessions get reused by the requests because the implementation of
Resource
is like aSingleton
and not like aFactory
(or that's what I guess that's happening here)
What complications could arise when using this?
Yes, it's possible, but the session per request is working correctly with the implementation I have, the issue appears when you have a bunch of requests at the same time, the sessions get reused by the requests because the implementation of
Resource
is like aSingleton
and not like aFactory
(or that's what I guess that's happening here)
How do you test this?
Hi! I'm trying to handle SQLAlchemy sessions as a resource provider instead of using FastAPI's built-in
Depends
.Let me share some code:
The database class:
The main container where I create these resources:
So, I inject the session into the services directly (or in the future in one repository). I don't like injecting the session into the routers directly that's why I prefer not using the FastAPI
Depends
.The code provided works as expected, it creates 1 session per request and then closes it, the thing is that this doesn't work in a concurrent environment, I made some local testing with locust, and once I start having many requests executing with multi-threads (I'm using
sync
endpoints), the sessions don't work as expected, it seems that the same session is being used in more than 1 request at the same time, so I guess this is something that we could solve if we had something likeFactoryResource
instead of just aResource
that works as aSingleton
. I tried to implement this myself by creating aCustomProvider
but I couldn't make it, so if anyone has any advice I'd appreciate it.P.S: I checked all the related issues regarding FastAPI and SQLAlchemy sessions in this repo and tried some approaches but none of them worked correctly