title: apscheduler Lost connection to MySQL server during query
body: <p>I use apscheduler to execute regular job, and I got some error on it.</p>
  
  <p>"Lost connection to MySQL server during query"</p>
  
  <p>To find the answer, I try some test on it, and found out if my database(MySQL) "wait_timeout" is less than schedule interval time then this error occur.
  (sorry here I made some mistake...is less than...)</p>
  
  <p>in the test: </p>
  
  <ul>
  <li><p>my job setting</p>
  
  <p>scheduler.add_job(period_job, 'interval', minutes=5, id='my_job_id')</p></li>
  <li><p>my database setting</p>
  
  <p>wait_timeout = 60</p></li>
  <li><p>my test code</p>
  
  <pre><code>from apscheduler.schedulers.background import BackgroundScheduler
  from flask import Flask
  app = Flask(__name__)
  
  scheduler = BackgroundScheduler({'apscheduler.jobstores.default': {
          'type': 'sqlalchemy',
          'url': 'mysql+pymysql://user:pass@url:3306/test_apscheduler?charset=utf8'
      },
          'apscheduler.executors.default': {
              'class': 'apscheduler.executors.pool:ThreadPoolExecutor',
              'max_workers': '20'
      },
          'apscheduler.executors.processpool': {
          'type': 'processpool',
          'max_workers': '5'
      },
          'apscheduler.job_defaults.coalesce': 'false',
          'apscheduler.job_defaults.max_instances': '3',
          'apscheduler.timezone': 'UTC',
      })
  
  scheduler.start()
  
  
  @app.route('/')
  def hello_world():
      scheduler.add_job(period_job, 'interval', minutes=5, id='my_job_id')
      return 'Hello World!'
  
  
  def period_job():
      print("hihi")
  
  
  if __name__ == '__main__':
      app.run()
  </code></pre></li>
  </ul>
  
  <p>total error message:</p>
  
  <pre><code>Exception in thread APScheduler:
  Traceback (most recent call last):
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context context)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute cursor.execute(statement, parameters)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 158, in execute result = self._query(query)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 308, in _query conn.query(q)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 820, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1002, in _read_query_result result.read()
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1285, in read first_packet = self.connection._read_packet()
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 946, in _read_packet packet_header = self._read_bytes(4)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 982, in _read_bytes 2013, "Lost connection to MySQL server during query")
  pymysql.err.OperationalError: (2013, 'Lost connection to MySQL server during query')
  
  The above exception was the direct cause of the following exception:
  
  Traceback (most recent call last):
    File "C:\Python34\lib\threading.py", line 921, in _bootstrap_inner self.run()
    File "C:\Python34\lib\threading.py", line 869, in run self._target(*self._args, **self._kwargs)
    File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\schedulers\blocking.py", line 27, in _main_loop wait_seconds = self._process_jobs()
    File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\schedulers\base.py", line 801, in _process_jobs for job in jobstore.get_due_jobs(now):
    File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\jobstores\sqlalchemy.py", line 69, in get_due_jobs return self._get_jobs(self.jobs_t.c.next_run_time &lt;= timestamp)
    File "C:\Users\skuo\apshcduler\lib\site-packages\apscheduler\jobstores\sqlalchemy.py", line 131, in _get_jobs for row in self.engine.execute(selectable):
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1991, in execute return connection.execute(statement, *multiparams, **params)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 914, in execute return meth(self, multiparams, params)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\sql\elements.py", line 323, in _execute_on_connection return connection._execute_clauseelement(self, multiparams, params)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1010, in _execute_clauseelement compiled_sql, distilled_params
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1146, in _execute_context context)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1341, in _handle_dbapi_exception exc_info
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\util\compat.py", line 200, in raise_from_cause reraise(type(exception), exception, tb=exc_tb, cause=cause)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\util\compat.py", line 183, in reraise raise value.with_traceback(tb)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\base.py", line 1139, in _execute_context context)
    File "C:\Users\skuo\apshcduler\lib\site-packages\sqlalchemy\engine\default.py", line 450, in do_execute cursor.execute(statement, parameters)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 158, in execute result = self._query(query)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\cursors.py", line 308, in _query conn.query(q)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 820, in query self._affected_rows = self._read_query_result(unbuffered=unbuffered)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1002, in _read_query_result result.read()
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 1285, in read first_packet = self.connection._read_packet()
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 946, in _read_packet packet_header = self._read_bytes(4)
    File "C:\Users\skuo\apshcduler\lib\site-packages\pymysql\connections.py", line 982, in _read_bytes 2013, "Lost connection to MySQL server during query")
  sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (2013, 'Lost connection to MySQL server during query') [SQL: 'SELECT apscheduler_jobs.id, apscheduler_jobs.job_state \nFROM apscheduler_jobs \nWHERE apscheduler_jobs.next_run_time &lt;= %(next_run_time_1)s ORDER BY apscheduler_jobs.next_run_time'] [parameters: {'next_run_time_1': 1457445220.361246}]
  </code></pre>
  
  <p>does anyone know what happened to this? and how to fix it?</p>