SQL Server · CrudLabDB
Batch insert
Feature 14: save many rows at once, in one transaction. If one row fails, nothing is saved (rollback).
JavaBatchInsertService
jdbc.batchUpdate(INSERT_SQL, rows, 500, (ps, r) -> {
ps.setString(1, r.code());
ps.setString(2, r.title());
...
});
// or, with JPA:
repo.saveAll(entities);
repo.flush();
- The method is
@Transactional: a duplicate code throws, and every row of the batch is rolled back. - SQL Server allows at most 2100 parameters per statement, so the "already exists?" check runs
WHERE code IN (…)in chunks of 1000. - Try to insert the sample CSV twice: the second time fails, unless you tick "Skip rows…".