Skip to content
Advertisement

Flask-sqlalchemy: When to close sessions?

I have created a python webapp with Flask and it seems like I am having connection issues with the database. I think this is because I don’t close my sessions somewhere in my code.

I have

JavaScript

for the database and use

JavaScript

for adding records to the database.

When do I have to close my session in this case? Is there a way to close the connection after the user leaves? Should I close every time a page is done with the database? Do I need to close my connection after a query?

Advertisement

Answer

The documentation says next:

As in the declarative approach, you need to close the session after each request or application context shutdown. Put this into your application module:

JavaScript

UPD: In case of Flask-SQLAlchemy this staff is hardcoded, thus you don’t need to care about it when develop.

Advertisement