Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
1
Indexable
Never
/*we process the data in batches of 1000 items at a time, and commit the changes for each batch. 
We also start a new transaction every 10 batches, to help reduce the memory usage.

Note that this is just one example of how you can implement batch processing. 
The actual batch size and number of transactions will depend on your specific use case and the available resources. */

int batchSize = 1000;
int totalReports = reportStorage.size();
int numBatches = (int) Math.ceil((double) totalReports / batchSize);

HibernateConfig.getInstance().begin();
RunDetails runDetails = HibernateConfig.getSession().get( RunDetails.class , runId);

for (int i = 0; i < numBatches; i++) {
  int startIndex = i * batchSize;
  int endIndex = Math.min(startIndex + batchSize, totalReports);
  Collection<ReportStorage> batchReports = reportStorage.subList(startIndex, endIndex);

  for (ReportStorage repStore : batchReports) {
    // process report
  }

  Collection<ReportStorage> existingReports = runDetails.getReports();
  existingReports.addAll(batchReports);
  runDetails.setReports(existingReports);
  // set other properties

  HibernateConfig.getSession().saveOrUpdate(runDetails);

  if ((i + 1) % 10 == 0) {
    HibernateConfig.getInstance().commit();
    HibernateConfig.getInstance().begin();
  }
}

HibernateConfig.getInstance().commit();