我有一个消费者可以说他不想处理更多的数据。
我看过Spring中的方法,但我没有办法在回调时通知JdbcTemplate我不想处理更多的行。
我可以抛出一个异常,但是jdbcTemplate.query(...)会重新抛出这个异常,这是我不希望发生的行为。
有没有办法强制停止处理行而不导致异常?
jdbc.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException
{
// TODO how to stop if I want to do it here?
}}
);发布于 2020-07-02 04:43:18
(如果有人来到这个帖子)-使用ResultSetExtractor。在while条件中循环结果集,并在需要时中断。
Set<String> distinctNames = jdbcTemplate.query(query,
new ResultSetExtractor<Set<String>>(){
public Set<String> extractData(ResultSet rs) throws SQLException, DataAccessException {
Set<String> distinctNames = new HashSet<>();
Integer cacheMax = 1000
while(rs.next() && distinctNames.size() <= cacheMax){
// magic is in while loop condition to break out when we want ignoring rest of results from DB.
distinctNames.add(rs.getString("COLUMN_NAME"));
}
return distinctNames;
}
});发布于 2018-04-27 18:22:47
看一看:
当此语句生成的ResultSet对象需要更多行时,JDBC jdbcTemplate.setFetchSize(intValue):会提示
驱动程序应该从数据库中提取多少行。如果指定的值为零,则忽略该提示。默认值为零。
如果超过限制,则会自动删除超出的行数。
我认为jdbcTemplate.setMaxRows(intValue)可能对您的需求有所帮助。
https://stackoverflow.com/questions/50060120
复制相似问题