Answer the question
In order to leave comments, you need to log in
At what point do you need to connect to the database in the flask application?
Hey!
I have an application written in flask. I am using Postgres database. Accordingly, the psycopg2 library
So the question arose: when should I use a connection to the database? at the very beginning, before all @app.route or in each @app.route do a connection and close the connection before return?
This question arose because I began to receive the following error:
psycopg2.errors.InFailedSqlTransaction: current transaction is aborted, commands ignored until end of transaction block
Answer the question
In order to leave comments, you need to log in
It is better to make one connection for the whole code. It makes no sense to make a connection for each route separately.
Familiarize yourself with Flask contexts.
Maybe this will also help
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question