mysql - Dynamically connect to different DB if one fails in Flask-SQLAlchemy -
i want find way can dynamically connected secondary db if primary db timed out. i've reserarched extensively , found solutions uses sqlalchemy not flask-sqlalchemy. recommended solution below.
engine1 = create_engine(...) engine2 = create_engine(...) session1 = sessionmaker(bind=engine1) session2 = sessionmaker(bind=engine2) session1 = scoped_session(session1, scopefunc=...) session2 = scoped_session(session2, scopefunc=...)
and want build fail safe below.. synthax in psuedo code.
if not request_has_connection: db = sqlalchemy(app) # instantiate db different config has different db uri.
the problem that, flask-sqlalchemy not allow users separately instantiate engine, db used every model instantiated 1 line db=sqlalchemy(app)
how can dynamically change uri of database secondary if primary has timedout or if lost connection?
thanks
maybe application factories (here)? if bind app instance db using lazy method can load different configuration object / file. not sure project structure right -
db = sqlalchemy()
and after
app = flask(__name__) db.init_app(app)
check out (here) flask snippet on lazy sqlalchemy setup.
Comments
Post a Comment