reddy
reddy

Reputation: 41

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.

Upvotes: 2

Views: 2441

Answers (1)

Ismail
Ismail

Reputation: 3042

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
  }
}

Upvotes: 2

Related Questions