Currently our application uses Spring, SimpleJdbcTemplate & MySQL.
DataSource used is org.apache.commons.dbcp.BasicDataSource with url properties “rewriteBatchedStatements=true”.
During the batch insert with SimpleJdbcTemplate.batchUpdate(List<Object[]>), there will be duplicate records(based on the primary key of the table we are inesrting) in the input batch.
Under the duplicate record scenario is it possible to
1) Insert all the non-duplicate records and get the response about the number of successful inserts?
OR
2) Completely rollback the batchInsert, no record should be inserted?
We are able to partially achieve the first requirement using the “INSERT IGNORE” of mysql. But the SimpleJdbcTemplate.batchUpdate() returns every record as updated.(Not able to capture only the inserted record count ignoring the duplicates)
And to achieve the second requirement we have to turn off the “rewriteBatchedStatements”. But this parameter has been fine tuned after performance test. So we can’t set this to “false”.
Is the possible to achieve one of two above cases, within the constraints of the components we are using as mentioned in the first line?
I am new to spring and jdbc world, so a detailed explanation will help us better.
Thanks
=> Well you can, insert all non.duplicate records, and get the response.
batchUpdate()returnsint[]i.e. array of integers which represents the numbers of rows affected by each update in the batch (inserted/updated).=> As batchInsert will be within single transaction, either all records will be inserted or no record is inserted. If transaction gets committed all records will be inserted. If any exception occurs, transaction will be rolled back automatically if you are using spring transaction management (either using
@Transactionalannotation orspring aop based tx advice). Here make sure, you setBasicDataSource.defaultAutoCommit = false.