This is how I am doing bulk inserts using EntityManager in JPA. Batch size is currently set to 50. I cannot call commit for each batch size. Either the whole transaction should be committed, or nothing at all.
The method bulkInsert() is being called by a separate method in another class and marked as @Transactional
How can I make the inserts much faster? I am inserting upto 200K rows.
class MyRepo{
@PersistenceContext
private EntityManager em;
@Value(value = "${batch_size}")
private int batchSize;
@Override
public List<Object> bulkInsert(List<Object> objects) {
IntStream.range(0, objects.size()).forEach(index -> {
em.persist(objects.get(index));
if (index + 1 % batchSize == 0) {
em.flush();
em.clear();
}
});
em.flush();
em.clear();
return objects;
}
}
I also have the following properties set to avoid the postgres connectivity loss, which I still face. The connection closes by itself inspite of the properties below.
spring.datasource.test-while-idle=true
spring.datasource.test-on-borrow=true
spring.datasource.validation-query=SELECT 1
spring.datasource.tomcat.validation-interval=0