1

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?

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

1 Comment

No. Inject a TransactionManager for Spring
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.