Psycopg2 with flask when to close the connection

I am trying to create a simple web application that will request db postgres and insert / delete data. Since this is a very simple application, I do not use an ORM layer like sqlalchemy. Instead, I would like to use psycopg directly. Now, I wonder when is the best time to close cursors and links? I am having trouble getting more details on when a connection is idling regarding access to a web application.

Thank!

+5
source share
1 answer

official documentation may be helpful

@app.before_request
def before_request():
   g.db = connect_db()

@app.teardown_request
def teardown_request(exception):
    g.db.close()
+9
source

All Articles