title: SQLAlchemy not closing sessions with ORM?
body: <p>I have a week end project with <a href="http://bottlepy.org/docs/dev/" rel="nofollow">Bottle.py</a> (0.10.11) and <a href="http://www.sqlalchemy.org/" rel="nofollow">SQLAlchemy</a>(0.7.9) using MySQL as a backend.</p>
  
  <p>I had a lot of "MySQL server has gone away" and drilled it down to the fact that some sessions would be left opened during the night while I was not using my program.</p>
  
  <p>Now I can see where the MySQL session is left open but I don't know how I should proceed.</p>
  
  <p>This is what I have in my router page web.py</p>
  
  <pre><code>[...]
  db = create_engine('mysql://USER:PASSWORD@DATABASE', poolclass=NullPool)
  session = scoped_session(sessionmaker(bind=db))
  
  @route("/")
      links = session.query(Link)
  
      session.close()
      return bottle.template("index", links=links)
  </code></pre>
  
  <p>in my view index.tpl</p>
  
  <pre><code>[...]
  %for link in links:
      &lt;div class="link"&gt;
          &lt;a class="link" href="{{link.url}}"&gt;{{link.title}}&lt;/a&gt; 
          %for tag in link.tags:
              &lt;a href="/tag/{{tag.text}}" class="tag"&gt;{{tag.text}}&lt;/a&gt;
          %end
          &lt;a href="/edit/{{link.id}}"&gt;edit&lt;/a&gt;
      &lt;/div&gt;
  %end
  [...]
  </code></pre>
  
  <p>If I use <code>session.query(Link).all()</code> instead of <code>session.query(Link)</code> the MySQL sessions are closed properly, but I can't benefit from the ORM factor.</p>
  
  <p>How can I close all session ? </p>
  
  <p>What am I doing wrong ?</p>