title: Clean-up database connection with SQLAlchemy in Pandas
body: <p>With Pandas, I can very easily read data from a database into a dataframe:</p>
  
  <pre><code>from sqlalchemy import create_engine
  import pandas
  
  
  query = 'SELECT * FROM Table_Name;'
  engine = create_engine('...')
  
  df = pandas.read_sql_query(query, engine)
  
  print(df.head())
  </code></pre>
  
  <p>I would like to make sure that no connection is kept open after executing <code>.read_sql_query()</code>, no matters if the query succeeded or if it raised an exception.</p>
  
  <p>I am currently:</p>
  
  <ul>
  <li>Using a function to restrict the engine's scope. I only expect to call this function once each half an hour, so I do not mind re-creating the engine if that helps ensuring everything is cleaned/closed/garbage-collected.</li>
  <li>Disabling pooling with <code>poolclass=NullPool</code>.</li>
  <li>Finally calling <code>engine.disponse()</code>.</li>
  </ul>
  
  <p>Like so:</p>
  
  <pre><code>from sqlalchemy import create_engine
  from sqlalchemy.pool import NullPool
  import pandas
  
  
  def get_data():
      query = 'SELECT * FROM Table_Name;'
      try:
          engine = create_engine('...', poolclass=NullPool)
          df = pandas.read_sql_query(query, engine)
      finally:
          engine.dispose()
      return df
  
  
  print(get_data().head())
  </code></pre>
  
  <p>Is there a better way?</p>