Flask SqlAlchemy MySQL connection timed out due to QueuePool overflow limit
Question: Please I need help with the following error which I get on the 16th database
connection. None of the other answers on Stackoverflow seem to work:

    
    
    QueuePool limit of size 5 overflow 10 reached, connection timed out, timeout 30
    

Backend configuration:

  * Python 2.6.9 
  * Flask 0.10.1 
  * Flask-SQLAlchemy 2.1
  * Mysql-connector-python 1.0.12 
  * Mysql 5.6.27

Database Setup:

    
    
    connection_str = 'mysql+mysqlconnector://%s:%s@%s:%s/%s' % (config["DATABASE_USER"], config["DATABASE_PASSWORD"], \
                                                                config["DATABASE_HOST"], config["DATABASE_PORT"], \
                                                                config["DATABASE_SCHEMA1"])
    engine = create_engine(connection_str, convert_unicode=True, pool_recycle=config["DATABASE_POOL_RECYCLE"])
    db_session = scoped_session(sessionmaker(autocommit=False,
                                             autoflush=False,
                                             bind=engine))
    
    Base = declarative_base()
    Base.query = db_session.query_property()
    
    import application_package.models
    Base.metadata.create_all(bind=engine)
    
    
    @app.teardown_appcontext
    def shutdown_session(exception=None):
        db_session.remove()
    


Answer: I've realized the problem is that I created a separate threadpool with threads
that weren't terminating and were keeping all my database connections open
even after the response has been returned to the client. This was a bad hack
and terrible idea. I intend to get rid of this threadpool and use celery to
schedule asynchronous tasks.

