This repository contains example code for how one might use Spring's JdbcTemplate with the Java 8 Stream API.
Sample code
JdbcStream.streamableQuery()
: an extension of JdbcTemplate to make aCloseable
streamable query. This gets into the protected guts ofJdbcTemplate
to manage a connection's lifetime, and becomes a resource which must be closed.JdbcStream.streamQuery()
: a callback-style query interface allowing stream processing insideJdbcTemplate
's own resource management system. This version will not work for empty result sets.JdbcStreamApplication.streamer
: a bean implementingstreamQuery()
as a consumer of aJdbcTemplate
. This one correctly handles empty result sets, and is the basis of the [gist] and [blog post] which this code fed.
There's a performance test suite which will go out of memory for query methods which inadvertently cache the entire result set in memory, and a basic test suite demonstrating the streaming code performs as expected.
The test cases show what not to do with this interface: they reimplement trivial SQL queries in Java code.