title: Dynamically connect to different DB if one fails in Flask-SQLAlchemy
body: <p>I want to find a way where I can dynamically be connected to secondary DB if primary DB is timed out. I've reserarched extensively and found some solutions that uses only the SQLAlchemy not the Flask-SQLAlchemy. The recommended solution was like below.</p>
  
  <pre><code>engine1 = create_engine(...)
  engine2 = create_engine(...)
  Session1 = sessionmaker(bind=engine1)
  Session2 = sessionmaker(bind=engine2)
  session1 = scoped_session(Session1, scopefunc=...)
  session2 = scoped_session(Session2, scopefunc=...)
  </code></pre>
  
  <p>And I want to build a fail safe something like below.. the synthax is in psuedo code.</p>
  
  <pre><code>if not request_has_connection:
     db = SQLAlchemy(app) # instantiate db with different config that has different db URI.
  </code></pre>
  
  <p>The problem is that, Flask-SQLAlchemy does not allow users to separately instantiate engine, the db that is used by every model is all instantiated from one line which is <code> db=SQLAlchemy(app) </code></p>
  
  <p>How can I dynamically change the URI of the database to secondary if the primary has timedout or if it lost its connection?</p>
  
  <p>Thanks</p>