title: Threading and SQLAlchemy Sessions
body: <p>I know this question has been asked a few times, but I'm still unsure about the usage of <code>scoped_session</code> with threading. Basically, I have an application with 10 worker threads. I have one <code>Engine</code> with a connection pool size of 11. Each thread has it's own session, and no information needs to be shared across the threads' sessions (if they could, that would be nice, but I have created a workaround). Finally, I am using the SQLAlchemy Core in the main thread for complex SQL statements, which is why I have 11 threads in the connection pool.</p>
  
  <p>I am using MySQL and my <code>pool_recycle</code> is set to 3600. I keep getting errors:</p>
  
  <p><code>(OperationalError) (2013, 'Lost connection to MySQL server during query')</code></p>
  
  <p>This has never happened when I had just one worker thread, even without any <code>pool_recycle</code> set. I have a very basic understanding of MySQL and SQLAlchemy, so I am not sure if my issues stem from my use of SQLAlchemy or MySQL (or none of the above).</p>
  
  <p>Here is my setup:</p>
  
  <pre><code>common = Common()
  class Common(object):
      def __init__(self):
      ...
           self.engine = create_engine(
              '%(type)s://%(username)s:%(password)s@%(endpoint)s:%(port)s/%(name)s?charset=utf8' % {
                'type': config.get('db_type'),
                'username': 'foo',
                'password': 'bar',
                'endpoint': config.get('db_endpoint'),
                'port': str(config.get('db_port')),
                'name': config.get('db_name'),
              },
          encoding='utf-8',
          pool_size=config.get('num_workers') + 1,
          pool_recycle=3600,
      )
      self.session = sessionmaker(bind=self.engine)
  </code></pre>
  
  <p>Each worker calls <code>self.session = common.session()</code> and uses this session throughout.</p>