Edi
Edi

Reputation: 327

Batch insert using spring data

I have 60K records to be inserted. I want to commit the records by batch of 100.

Below is my code

for(int i = 0 ;i < 60000; i++) {
   entityRepo.save(entity);
   if(i % 100 == 0) {
      entityManager.flush();
      entityManager.clear();
      LOG.info("Committed = " + i);
   } 
}
entityManager.flush();
entityManager.clear();

I keep checking the database whenever I receive the log but I don't see the records getting committed.. What am I missing?

Upvotes: 1

Views: 5538

Answers (2)

Vinay Veluri
Vinay Veluri

Reputation: 6855

I assume two ways to do this, One as define transaction declarative, and call from external method.

Parent:

 List<Domain> domainList = new ArrayList<>();
 for(int i = 0 ;i < 60000; i++) {
    domainList.add(domain);
    if(i%100 == 0){
    child.saveAll(domainList);
    domainList.clear();
    }
 }

Child:

@Transactional
public void saveAll(List<Domain> domainList) {
}

This calls the declarative method at regular intervals as defined by the parent.

The other one is to manually begin and end the transaction and close the session.

Upvotes: 1

Raffaele
Raffaele

Reputation: 20885

It is not enough to call flush() and clear(). You need a reference to the Transaction and call .commit() (from the reference guide)

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<100000; i++ ) {
    Customer customer = new Customer(.....);
    session.save(customer);
}
tx.commit();
session.close();

Upvotes: 2

Related Questions