title: SQLAlchemy Connection Pool and Sessions
body: <p>I've recently started using <code>SQLAlchemy</code> and am trying to understand how the <code>connection pool</code> and <code>session</code> work in a <code>web-application</code></p>
  
  <p>I am building an <code>API</code> using <code>flask</code>. </p>
  
  <p><strong>__init__.py</strong></p>
  
  <pre><code>engine = create_engine('mysql://username:password@localhost/my_database') 
  DBSession = sessionmaker(bind=engine)
  </code></pre>
  
  <p><strong>views.py</strong></p>
  
  <pre><code>@app.route('/books', methods = ['GET'])
  def getBooks():
      session = DBSession()
      returnedBooks = session.query(BOOK).all()
      session.close()
  
      return something...
  </code></pre>
  
  <p>1) Firstly, if I don't explicitly close my <code>session</code> object, will it automatically close after the <code>request</code> has been processed?</p>
  
  <p>2) When my app receives multiple <code>requests</code>, are the multiple <code>session</code> objects being created all linked to the single <code>engine</code> object that was created in my <code>__init__.py</code> file?</p>
  
  <p>3) Are the <code>session</code> objects being created in <code>view.py</code> the <code>connections</code> that the <code>connection pool</code> holds? If so, and these weren't closed, then would new connections have to be made for subsequent requests?</p>
  
  <p>4) Should I be using <code>dispose()</code> at some point?</p>