58
Reading the documentation is recommended:
the
scoped_session()
function is provided which produces a thread-managed registry ofSession
objects. It is commonly used in web applications so that a single global variable can be used to safely represent transactional sessions with sets of objects, localized to a single thread.
In short, use scoped_session()
for thread safety.
12
Scoped_session at every method will give you a thread of local session which you cannot obtain beforehand (like at the module level).It’s not needed to open a new session in every method, You can use a global session , Create a session only when the global session is not available. i.e you can write a method which returns a session and add it to the init.py inside your package.
- [Django]-AttributeError: 'module' object has no attribute 'tests'
- [Django]-Using Pylint with Django
- [Django]-Check if OneToOneField is None in Django
9
Don’t use scoped_session and don’t use Flask-SQLAlchemy.
Just use Session = sessionmaker()
held in a singleton/service class, and use session = Session()
on every HTTP request to guarantee that a fresh connection is provided.
Thread Local storage is clumsy and involves holding state which doesn’t play nicely with different web-server threading models. Better to stay stateless. See for example SqlAlchemy’s documentation here mentioning not to forget to call .remove()
if you are using scoped_session
. Will anyone remember to do that?
Below is an excerpt from https://docs.sqlalchemy.org/en/14/orm/contextual.html#using-thread-local-scope-with-web-applications:
Using the above flow, the process of integrating the Session with the web application has exactly two requirements:
Create a single
scoped_session
registry when the web application first starts, ensuring that this object is accessible by the rest of the application.Ensure that
scoped_session.remove()
is called when the web request ends, usually by integrating with the web framework’s event system to establish an “on request end” event.
- [Django]-Sending an SMS to a Cellphone using Django
- [Django]-How to get form fields' id in Django
- [Django]-Django migrations RunPython not able to call model methods
4
FYI, when using flask-sqlalchemy, the session object provided is by default a scoped session object.
http://flask-sqlalchemy.pocoo.org/2.3/quickstart/#road-to-enlightenment
- [Django]-How to get the username of the logged-in user in Django?
- [Django]-Images from ImageField in Django don't load in template
- [Django]-How to make two django projects share the same database
3
I am looking into this myself, but I am not an expert.
My three points are:
- SQLAlchemy docs provide a proposed approach using
scoped_session
, per Mr. Kluev’s comment above, at this link: http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#using-thread-local-scope-with-web-applications. - At that web location, the SQLAlchemy docs also say that it is “…strongly recommended that the integration tools provided with the web framework itself be used, if available, instead of
scoped_session
.” - Flask-SQLAlchemy, for example, appears to claim that it takes care of this: http://pythonhosted.org/Flask-SQLAlchemy/quickstart.html#a-minimal-application
- [Django]-Django model manager objects.create where is the documentation?
- [Django]-How do I use CSS in Django?
- [Django]-Where does pip install its packages?