I am using sqlite as db in my Micronaut application getting the sqlite busy error in the below code:
@SneakyThrows
@TransactionalAdvice(value = EmpDao.DATASOURCE, propagation = TransactionDefinition.Propagation.REQUIRES_NEW)
public void storeEmp(EmpDto empDto) {
String id = empDto.getId();
try {
if (empDao.existsById(id)) {
log.debug("updating emp for id {}", empDto.getId());
empDao.update(EmpEntity.builder()
.id(id)
.data(getJson(empDto))
.entryCreatedAt(timeService.nowDateTime().toEpochSecond(ZoneOffset.UTC))
.build());
} else {
empDao.save(
EmpEntity.builder()
.id(id)
.data(getJson(empDto))
.entryCreatedAt(timeService.nowDateTime().toEpochSecond(ZoneOffset.UTC))
.build());
}
}catch(Exception e){
log.error("emp db save/update failed for id {} ",id, e);
}
}
@SneakyThrows
@TransactionalAdvice(value = EmpDao.DATASOURCE, propagation = TransactionDefinition.Propagation.REQUIRES_NEW)
public void storeEmployees(List<Emp> empDtos) {
try {
empDao.saveAll(empDto);
} catch (Exception ex) {
log.warn("saveAll failed", ex);
empDtos.forEach(this::storeEmp);
}
}
In stacktrace I can see first saveAll
getting failed becuase of Primary keyconstraint Issue, that might be because of duplicate emp ids in the list
SQL error executing INSERT: [SQLITE_CONSTRAINT_PRIMARYKEY] A PRIMARY KEY constraint failed
and After that when It tried to save/update each emp object independently through storeEmp method in forEach, it's failing with the sqlite busy exception.
What I am not sure if saveAll is already failed, how there can be multiple connection to sqlite. Can anyone suggest what's wrong with the above code.
Thanks