简体   繁体   English

列表小于 getBatchSize() 的 jdbcTemplate.batchUpdate() 抛出 IndexOutOfBoundsException 错误

[英]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.

相关问题 有关大量查询的jdbcTemplate.batchUpdate()问题 - Issue with jdbcTemplate.batchUpdate() for large list of queries jdbcTemplate.batchUpdate 跳过策略 - jdbcTemplate.batchUpdate Skip policy jdbctemplate.batchupdate 是多线程的还是并发的? - Is jdbctemplate.batchupdate multithreaded or concurrent? 批量插入使用 jdbcTemplate.batchUpdate 混淆 - Batch insert using jdbcTemplate.batchUpdate confusion 为 jdbcTemplate.batchUpdate() 方法编写单元测试 - Write unit test for jdbcTemplate.batchUpdate() method 如何包括jdbctemplate.batchUpdate(String sql,List的两个参数 <Object[]> batchArgs)? - How to include two parameters for jdbctemplate.batchUpdate(String sql, List<Object[]> batchArgs)? 为什么 Spring 的 jdbcTemplate.batchUpdate() 这么慢? - Why Spring's jdbcTemplate.batchUpdate() so slow? 使用spring“ jdbcTemplate.batchUpdate”进行动态插入查询的批处理 - batch processing with dynamic insert query using spring “jdbcTemplate.batchUpdate” JdbcTemplate.batchUpdate() 在一项插入错误时返回 0,但将剩余项插入 sql 服务器数据库,尽管使用 @Transactional - JdbcTemplate.batchUpdate() returns 0, on insert error for one item but inserts the remaining item into sql server db despite using @Transactional 有什么更好的方法检查JdbcTemplate.batchUpdate中的名称字段,名称表的SQL注入? - What better way to check SQL injection of names field, names tables in JdbcTemplate.batchUpdate?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM