title: JdbcTemplate: how to read large tables?
body: <p>I am trying to dump database tables to a file using Spring jdbcTemplate. I can't use <code>pg_dump</code> or <code>psql</code> becase it's a custom data dump.</p>
  <p>There are some large tables that causes <code>OutOfMemoryError</code>.</p>
  <p>Theorically setting <code>fetchSize</code> to some number <code>&gt; 0</code> would not eat up the memory as <a href="https://jdbc.postgresql.org/documentation/head/query.html#fetchsize-example" rel="nofollow noreferrer">stated</a>.</p>
  <p>I have tried:
  <code>jdbcTemplate.setFetchSize()</code> and <code>rs.setFetchSize()</code>.</p>
  <pre><code>private void dumpTable(final String qry, final CSVPrinter csvPrinter) {
      jdbcTemplate.queryForStream(qry, (rs, rowNum) -&gt; getObjects(rs)).forEach(objs -&gt; {
          try {
              csvPrinter.printRecord(objs);
          } catch (IOException e) {
              throw new IllegalStateException(&quot;Error&quot;, e);
          }
      });
  }
  </code></pre>
  <p>Query looks  like this:</p>
  <pre><code>&quot;select %s from %s t where exists(select 1 from log.log_table l where l.schema = '%s' and l.table = '%s' and l.key = t.%s and l.location in (0,%d))&quot;;
  </code></pre>
  <p>Also tried <code>jdbcTemplate.query()</code> with <code>RowCallbackHandler</code>.</p>