[英]jdbcTemplate.batchUpdate() with smaller list than getBatchSize() throws IndexOutOfBoundsException error
I'm calling jdbcTemplate.batchUpdate()
with chunks of 1000. The list can be arbitrarily large -- and sometimes it can be less than 1000, in which case there's just that one small batch.我正在使用 1000 个块调用jdbcTemplate.batchUpdate()
。该列表可以是任意大的——有时它可能小于 1000,在这种情况下只有一小批。 My assumption was jdbcTemplate.batchUpdate
would be smart enough to figure out how many batches to execute.我的假设是jdbcTemplate.batchUpdate
足够聪明, jdbcTemplate.batchUpdate
要执行多少批次。
But I'm seeing the following: suppose my list has 3 items:但我看到以下内容:假设我的列表有 3 个项目:
final List<UsersT> batchInsertUsers = ...; // 3 items in List
String sqlInsert = "INSERT INTO USERS_T ...";
jdbcTemplate.batchUpdate(sqlInsert, new BatchPreparedStatementSetter() {
@Override
public int getBatchSize() {
return 1000; // Max Batch size greater than current list. 1 batch expected
}
@Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
ps.setInt(1, batchInsertUsers.get(i).getId());
ps.setString(2, batchInsertUsers.get(i).getPassword());
// etc.
}
} }
Error:错误:
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.rangeCheck(ArrayList.java:657)
at java.util.ArrayList.get(ArrayList.java:433)
at app.ResearcherServiceImpl$1.setValues(ResearcherServiceImpl.java:535)
int getBatchSize()
Return the size of the batch. int getBatchSize()
返回批次的大小。
Use this implementation :使用这个实现:
@Override
public int getBatchSize() {
return batchInsertUsers.size(); // Return the size of the batch.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.