简体   繁体   中英

Spring boot JPA batch inserts Exception Handling

I am working on a real time use case that needs to load batch of messages into SQL server table with spring boot JPA adding all the model objects to the list and doing this for batch loads repository.saveAll(list) . Due to performance, I cannot do record by record inserts. I am looking for below option.

Is there any way I can read the error causing message and continue with other records to insert into table. I have to store this error message somewhere and shouldn't pass to Downstream applications.

The saveAll method, of Spring data JPA, is actually saving the list of records one by one:

/*
 * (non-Javadoc)
 * @see org.springframework.data.jpa.repository.JpaRepository#save(java.lang.Iterable)
 */
@Transactional
@Override
public <S extends T> List<S> saveAll(Iterable<S> entities) {

    Assert.notNull(entities, "Entities must not be null!");

    List<S> result = new ArrayList<S>();

    for (S entity : entities) {
        result.add(save(entity));
    }

    return result;
}

Still, the difference between handling the loop of messages yourself or let it to Spring data JPA to do the job: saveAll uses the property spring.jpa.properties.hibernate.jdbc.batch_size=4 to determine if batching is activated or not, and depending on the size of the batch, the insert can perform better.

I think you just need to loop on the messages yourself and catching the errors youself:

for(EnitityType entity: list){
  try{
    repository.save(entity);
  } catch (Exception e){
    // Do what you want
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM