title: MySQL Connection not available when use SQLAlchemy(MySQL) and Flask
body: <p>I'm getting this error sometime (sometime is ok, sometime is wrong):</p>
  
  <pre><code>sqlalchemy.exc.OperationalError: (OperationalError) MySQL Connection not available.
  </code></pre>
  
  <p>while using <code>session.query</code></p>
  
  <p>I'm writing a simple server with Flask and SQLAlchemy (MySQL). My <code>app.py</code> like this:</p>
  
  <pre><code>Session = sessionmaker(bind=engine)
  session = Session()
  
  @app.route('/foo')
  def foo():
      try:
          session.query(Foo).all()
      except Exception:
          session.rollback()
  </code></pre>
  
  <p><strong>Update</strong>
  I also create new <code>session</code> in another file and call it in <code>app.py</code></p>
  
  <pre><code>Session = sessionmaker(bind=engine)
  session = Session()
  
  def foo_helper(): #call in app.py
      session.query(Something).all()
  </code></pre>
  
  <p><strong>Update 2</strong>
  My <code>engine</code>:</p>
  
  <pre><code>engine = create_engine('path')
  </code></pre>
  
  <p>How can I avoid that error?</p>
  
  <p>Thank you!</p>