Why Spring's jdbcTemplate.batchUpdate() so slow?

后端 未结 8 2364
终归单人心
终归单人心 2020-12-04 18:11

I\'m trying to find the faster way to do batch insert.

I tried to insert several batches with jdbcTemplate.update(String sql), wher

8条回答
  •  时光取名叫无心
    2020-12-04 19:16

    Simply use transaction. Add @Transactional on method.

    Be sure to declare the correct TX manager if using several datasources @Transactional("dsTxManager"). I have a case where inserting 60000 records. It takes about 15s. No other tweak:

    @Transactional("myDataSourceTxManager")
    public void save(...) {
    ...
        jdbcTemplate.batchUpdate(query, new BatchPreparedStatementSetter() {
    
                @Override
                public void setValues(PreparedStatement ps, int i) throws SQLException {
                    ...
    
                }
    
                @Override
                public int getBatchSize() {
                    if(data == null){
                        return 0;
                    }
                    return data.size();
                }
            });
        }
    

提交回复
热议问题