I had a piece of code which was inserting data into 4 tables in DB one record at a time. The data comes from a file which has around 11K records on an average. With this code the complete processing is getting done in 10-15 min.
for (final LedgerEntry ledgerEntry : items) {
final EntityTransaction tx = entityManager.getTransaction();
tx.begin();
entityManager.persist(ledgerEntry);
tx.commit();
}
I have refactored the code to insert the data in one go so that if something goes wrong no data is inserted in DB which was not the case in above piece of code. My new code is taking around 10 min to insert data in DB in test env. but in Live it was working very slow and took around 6-7 hours for 9k records.
final EntityTransaction tx = entityManager.getTransaction();
if (!tx.isActive()) tx.begin();
for (final LedgerEntry ledgerEntry : items) {
entityManager.persist(ledgerEntry);
}
tx.commit();
Can anyone suggest what can be done to increase the performance as the earlier code is causing lot of issues incase file fails and inserts duplicate records