title: How can I stream/chunk database results asynchronously?
body: <p>I'd like to use an <code>Enumerator</code> (similar to the <a href="http://www.playframework.com/documentation/2.0.1/ScalaStream" rel="nofollow">ScalaStream</a> example) to chunk database results to the response as TSV. I may have many thousands of rows and i don't want to page the results nor do i want to accumulate the entire <code>ResultSet</code> into a single <code>String</code>. If needed, it is ok for chunks to not break at line separators. In other words, the only criteria is to chunk the results, not that it adhere to TSV delimiters.</p>
  
  <p>I'd like to do something like this in my Action:</p>
  
  <pre><code>Ok.stream(new Iterator[ResultSet] {
    val conn = DB.getConnection()
    val stmt = conn.createStatement
    val rs = stmt.executeQuery("""select * from atable""")
    def hasNext = !rs.isLast() &amp;&amp; !rs.isClosed()
    def next() = {
      if (!rs.next()) {
        conn.close()
        null
      } else rs
    }
  }.toStream)
  </code></pre>
  
  <p>Unfortunately, the <code>Ok.stream</code> expects a <code>java.io.InputStream</code> and going the route of wrapping the results in an <code>InputStream</code> seems excessive. <code>Ok.stream</code> also accepts an <code>Enumerator</code> but i'm not sure how to create one in this context.</p>