title: Is there a way to test an SQLAlchemy Connection?
body: <p>I'm using SQLAlchemy to connect to write a pandas DataFrame to a MySQL database. Early on in my code I create an SQLAlchemy engine:</p>
  
  <pre><code>engine = create_my_sqlalchemy_connection()
  </code></pre>
  
  <p>I execute some queries, do some calculations, and then try to use that same engine to write to the database a little later:</p>
  
  <pre><code>df.to_sql('my_table', engine, if_exists='append', index=False)
  </code></pre>
  
  <p>Sometimes this works, and sometimes the connection is lost by the time the code is ready to write to the DB, and there is an error.</p>
  
  <p>I could do a try, except and create a new connection if needed:</p>
  
  <pre><code>try:
      df.to_sql('my_table', engine, if_exists='append', index=False)
  except:
      engine = create_my_sqlalchemy_connection()
      df.to_sql('my_table', engine, if_exists='append', index=False)
  </code></pre>
  
  <p>However, I thought I'd reach out and see if anyone knows of a better way (e.g. if there is some SQLAlchemy method that I am unaware of for testing to see if the connection still exists).</p>