问题
Having list which contains data model objects and passing it to saveALL method of CRUD repository. Using spring boot jpa batch concept to insert bulk data at a time. While bulk insert, spring jpa batch failed due to exceptions then all records are rollback. How to find correct and incorrect records? and we need to insert valid records into table without rollback and insert invalid records into error table. Please help me.
public void execeBatch() {
try {
if(!dataList.isEmpty()) {
employeeRepository.saveAll(dataList);
}
}catch(Exception e) {
log.error("Exception while execeBatch..... and calling handleBatchExceptions()");
handleBatchExceptions();
}
}
public void handleBatchExceptions() {
for (EmployeeModel employeeModel : dataList) {
try {
employeeRepository.save(employeeModel);
} catch (Exception e) {
}
}
dataList.clear();
}
来源:https://stackoverflow.com/questions/58815646/how-to-handle-spring-boot-jpa-batch-execution-failed-transactions