@Override
public void bulkInsert(List<UserEntityPermission> permissions) {
int chunkSize = (int) Math.ceil((double) permissions.size() / THREADS);
ExecutorService executor = Executors.newFixedThreadPool(THREADS);
for (int i = 0; i < THREADS; i++) {
int start = i * chunkSize;
int end = Math.min(start + chunkSize, permissions.size());
List<UserEntityPermission> subList = permissions.subList(start, end);
executor.submit(() -> {
TransactionTemplate template = new TransactionTemplate(transactionManager);
template.execute(status -> {
EntityManager em = entityManagerFactory.createEntityManager();
try {
em.getTransaction().begin();
for (int j = 0; j < subList.size(); j++) {
em.persist(subList.get(j));
if (j > 0 && j % BATCH_SIZE == 0) {
em.flush();
em.clear();
}
}
em.flush();
em.clear();
em.getTransaction().commit();
} catch (Exception e) {
System.err.println("something went wrong " + e);
e.printStackTrace();
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
} finally {
em.close();
}
return null;
});
});
}
Created
September 22, 2025 20:24
-
-
Save KotaHemanthUC/f3591048d94ec68e26db29af2687f2d9 to your computer and use it in GitHub Desktop.
Author
KotaHemanthUC
commented
Oct 3, 2025
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment