@app.teardown_appcontext
def close_db(error):
    'Closes the database again at the end of the request.'
    if hasattr(g, 'db'):
        g.db.disconnect()
        if error:
            logger.error(('Database connection closed because of :' + str(error)))
Closes the database again at the end of the request.
@app.teardown_appcontext
def close_db(error):
    
    if hasattr(g, 'db'):
        g.db.disconnect()
        if error:
            logger.error(('Database connection closed because of :' + str(error)))