Last active
May 21, 2020 14:52
-
-
Save asbestos/1631002579f05fce06e0 to your computer and use it in GitHub Desktop.
JDBCTemplate prepared statement creator for large result sets
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class StreamingStatementCreator implements PreparedStatementCreator { | |
private final String sql; | |
public StreamingStatementCreator(String sql) { | |
this.sql = sql; | |
} | |
@Override | |
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException { | |
final PreparedStatement statement = connection.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); | |
statement.setFetchSize(Integer.MIN_VALUE); | |
return statement; | |
} | |
} | |
//Usage | |
DataSource dataSource = ...; | |
RowCallbackHandler rowHandler = ...; | |
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); | |
jdbcTemplate.query(new StreamingStatementCreator("SELECT * FROM huge_table"), rowHandler); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment