[QUESTION] SQLAlchemy Dependency vs. Middleware vs. scoped_session
See original GitHub issueThe SQLAlchemy library comes with a scoped_session
object, which effectively allows for thread-safe database sessions. Is there a reason the docs recommend using the dependency method > middleware method > just a scoped_session
global?
Issue Analytics
- State:
- Created 4 years ago
- Reactions:8
- Comments:29 (11 by maintainers)
Top Results From Across the Web
[QUESTION] SQLAlchemy Dependency vs. Middleware vs ...
The SQLAlchemy library comes with a scoped_session object, which effectively allows for thread-safe database sessions. Is there a reason the docs recommend ...
Read more >SQLAlchemy Sessions - FastAPI Utilities
The get_db dependency makes use of a context-manager dependency, rather than a middleware-based approach. This means that any endpoints that don't make use...
Read more >tiangolo/fastapi - Gitter
Is there a way to add support of application/json or any middleware to convert incoming json to www-form-urlencoded? I am in situation that...
Read more >python - scoped_session(sessionmaker()) or plain ...
I am using SQlAlchemy in my web project. What should I use - scoped_session(sessionmaker()) or plain sessionmaker() - and why?
Read more >SQL (Relational) Databases - FastAPI
Our dependency will create a new SQLAlchemy SessionLocal that will be used in a single request, and then close it once the request...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’m not sure about the reasoning behind the doc’s reccomendations, but the reason why I’d personally advise against making
scoped_session
global is because it then forces you to also have a globalengine
to bind it to, and global variables in ASGI don’t have a well-defined lifetime (see #617). There’s also how using dependencies rather than globals mean that all the ressource needed by your route are passed as function parameters, which tends to make testing easier since you can just pass a fake DB session instead of the one you would normally use.SQLAlchemy’s own doc says
scoped_session
are just one of many ways of handling sessions. From my understanding of howscoped_session
and sessions work, thread-local sessions might not work that well with FastAPI since Python’s coroutines don’t have a 1:1 mapping with threads, so I suspect that this could cause some unexpected behavior (though I haven’t tried this, I might be wrong).I personally prefer handling my connections this way:
The benefits of this approach are:
uvicorn --reload
I’ve been trying to go through all the related issues #104 #290 etc., and come up with a summary of what the best production worthy steps might be as of 2022. It looks like, the following apply:
lifespan
(app.state and a global reference are both useful to implement this feature)@sm-Fifteen , @dmontagu could you please weigh in with your thoughts? If this is a useful summary, it might be a good idea to write this in the docs.