title: Avoiding "MySQL server has gone away" on infrequently used Python / Flask server with SQLAlchemy
body: <p>How can Flask / SQLAlchemy be configured to create a new database connection if one is not present?</p>
  
  <p>I have an infrequently visited Python / Flask server which uses SQLAlchemy. It gets visited every couple of days, and on the first visit it often throws a "MySQL server has gone away" error. Subsequent page views are fine, but it looks unprofessional to have this initial error.</p>
  
  <p>I'd like to know the correct way to handle this - advice like "make a really long time out", which would be about 4 days long in this case, doesn't seem correct. How can I test for the lack of a database connection and create one if needed?</p>