[英]How do I stop processing rows in Spring-JDBC?
I have a consumer that can say he doesn't want to process more data.我有一个消费者可以说他不想处理更多数据。
I've looked upon the methods in Spring JdbcTemplate
, but I see no way to inform Spring I don't want to process more rows when I'm on callback.我已经查看了 Spring
JdbcTemplate
的方法,但是我看不到在回调时通知 Spring 我不想处理更多行的方法。
I could throw an exception, but then the jdbcTemplate.query(...)
would rethrow this exception, which is the behavior I don't wish.我可以抛出异常,但是
jdbcTemplate.query(...)
会重新抛出这个异常,这是我不希望的行为。
Is there any way to force stop processing rows without causing exception?有没有办法强制停止处理行而不会导致异常?
jdbc.query(sql, new RowCallbackHandler() {
@Override
public void processRow(ResultSet rs) throws SQLException
{
// TODO how to stop if I want to do it here?
}}
);
(If somebody comes to this thread) - Use ResultSetExtractor. (如果有人来到这个线程) - 使用 ResultSetExtractor。 Loop through resultset in while condition and break out when needed.
在 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;
}
});
Take a look into:看看:
jdbcTemplate.setFetchSize(intValue): Gives the JDBC driver a hint as to the number of rows that should be fetched from the database when more rows are needed for ResultSet objects genrated by this Statement.
jdbcTemplate.setFetchSize(intValue):当此语句生成的 ResultSet 对象需要更多行时,向 JDBC 驱动程序提供有关应从数据库中提取的行数的提示。 If the value specified is zero, then the hint is ignored.
如果指定的值为零,则忽略提示。 The default value is zero.
默认值为零。
jdbcTemplate.setMaxRows(intValue): Sets the limit for the maximum number of rows that any ResultSet object generated by this Statement object can contain to the given number.
jdbcTemplate.setMaxRows(intValue):将此 Statement 对象生成的任何 ResultSet 对象可以包含的最大行数设置为给定数。 If the limit is exceeded, the excess rows are silently dropped.
如果超出限制,多余的行将被静默删除。
I think that jdbcTemplate.setMaxRows(intValue) could be helpful for your requirements.我认为jdbcTemplate.setMaxRows(intValue)可能对您的要求有所帮助。
您是否最终弄清楚了这一点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.