title: Flask-SQLAlchemy: Can't reconnect until invalid transaction is rolled back
body: <p>So I am using Amazon Web Services RDS to run a MySQL server and using Python's Flask framework to run the application server and Flask-SQLAlchemy to interface with the RDS.</p>
  
  <p>My app config.py</p>
  
  <pre><code>SQLALCHEMY_DATABASE_URI = '&lt;RDS Host&gt;'
  SQLALCHEMY_POOL_RECYCLE = 60
  </code></pre>
  
  <p>My __ init __.py</p>
  
  <pre><code>from flask import Flask
  from flask.ext.sqlalchemy import SQLAlchemy
  
  application = Flask(__name__)
  application.config.from_object('config')
  db = SQLAlchemy(application)
  </code></pre>
  
  <p>I have my main application.py</p>
  
  <pre><code>from flask import Flask
  from application import db
  import flask.ext.restless
  from application.models import Person
  
  application = Flask(__name__)
  application.debug=True
  db.init_app(application)
  
  @application.route('/')
  def index():
      return "Hello, World!"
  
  manager = flask.ext.restless.APIManager(application, flask_sqlalchemy_db=db)
  manager.create_api(Person, methods=['GET','POST', 'DELETE'])
  
  if __name__ == '__main__':
      application.run(host='0.0.0.0')
  </code></pre>
  
  <p>The models.py</p>
  
  <pre><code>class Person(db.Model):
      __bind_key__= 'people'
      id = db.Column(db.Integer, primary_key=True)
      firstName = db.Column(db.String(80))
      lastName = db.Column(db.String(80))
      email = db.Column(db.String(80))
  
      def __init__(self, firstName=None, lastName=None, email=None):
          self.firstName = firstName
          self.lastName = lastName
          self.email = email
  </code></pre>
  
  <p>I then have a script to populate the database for testing purposes after db creation and app start:</p>
  
  <pre><code>from application import db
  from application.models import Person
  
  person = Person('Bob', 'Jones', 'bob@website.net')
  db.session.add(person)
  db.session.commit()
  </code></pre>
  
  <p>Once I've reset the database with db.drop_all() and db.create_all() I start the application.py and then the script to populate the database.</p>
  
  <p>The server will respond with correct JSON but if I come back and check it hours later, I get the error that I need to rollback or sometimes the 2006 error that the MySQL server has gone away.</p>
  
  <p>People suggested that I change timeout settings on the MySQL server but that hasn't fixed anything. Here are my settings:</p>
  
  <pre><code>innodb_lock_wait_timeout = 3000
  max_allowed_packet       = 65536
  net_write_timeout        = 300
  wait_timeout             = 300
  </code></pre>
  
  <p>Then when I look at the RDS monitor, it shows the MySQL server kept the connection open for quite a while until the timeout. Now correct me if I'm wrong but isn't the connection supposed to be closed after it's finished? It seems that the application server keeps making sure that the database connection exists and then when the MySQL server times out, Flask/Flask-SQLAlchemy throws an error and brings down the app server with it.</p>
  
  <p>Any suggestions are appreciated, thanks!</p>