E
E
Elvis2019-12-06 20:41:08
Flask
Elvis, 2019-12-06 20:41:08

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

This is when I often send post requests.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
L
lightmanLP, 2019-12-06
@Dr_Elvis

It is better to make one connection for the whole code. It makes no sense to make a connection for each route separately.

E
Evloshevsky Nikolay, 2020-01-10
@n1k_crimea

Familiarize yourself with Flask contexts.
Maybe this will also help

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question