Search code examples
springspring-bootspring-batchspring-jdbcjdbctemplate

JdbcBatchItemWriterBuilder vs org.springframework.jdbc.core.jdbcTemplate.batchUpdate


I understand jdbcTemplate.batchUpdate is used for sending several records to data base in one communication. Lets say i have 1000 records to be updated, instead of 1000 communications from Application to database, the Application will send 1000 records in request.

Coming to JdbcBatchItemWriterBuilder its combination of Tasks in a job.

My question is, if there is 1000 records to be processed(INSERT statements) via JdbcBatchItemWriterBuilder, all INSERTS executed in one go? or one after one?

If one after one, connecting to database 1000 times using JdbcBatchItemWriterBuilder causes perf issues? hows that handled?

i would like to understand if Spring batch performs better than running 1000 INSERT staments using jdbcTemplate.update ?


Solution

  • The JdbcBatchItemWriter uses java.sql.PreparedStatement#addBatch and java.sql.Statement#executeBatch internally (See https://github.com/spring-projects/spring-batch/blob/c4010fbffa6b71cbcfe79d523023251ce73666a4/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/database/JdbcBatchItemWriter.java#L189-L195), so there will be a single batch insert for all items of the chunk.

    Moreover, this will be executed in a single transaction as described in the Chunk-oriented Processing section of the reference documentation.