To preserve the database connection within the same flask session you may use the application context and assign database connection.
If the connection breaks,the context will try to establish the connection back as it polls the connection object continuously.
from flask import Flask
application = Flask(__name__)
def connect_to_database():
db_handler = SqliteDBConnect("uid={0};""pwd={1}".format(UID, PWD),
table_prefix="{}".format(TBL_PRFX))
return db_handler
fd = {'_database': None}
def get_db():
db = fd['_database']
if not isinstance(db, SqliteDBConnect):
fd['_database'] = connect_to_database()
db = fd['_database']
return db
with application.app_context():
#Get DB connection from application's context
db = LocalProxy(lambda: get_db())