Massive insert with JPA + Hibernate

前端 未结 3 429
悲哀的现实
悲哀的现实 2020-12-15 09:22

I need to do a massive insert using EJB 3, Hibernate, Spring Data and Oracle. Originally, I am using Spring Data and code is below:

talaoAITDAO.save(taloes);         


        
3条回答
  •  猫巷女王i
    2020-12-15 09:45

    A couple of things.

    First your configuration properties are wrong order_inserts must be hibernate.order_inserts . Currently your setting is ignored and you haven't changed a thing.

    Next use the EntityManager instead of doing all that nasty hibernate stuff. The EntityManager also has a flush and clear method. This should at least cleanup your method. Without the order this helps a little to cleanup the session and preventing dirty-checks on all the objects in there.

    EntityManager em = getEntityManager();
    int batchSize = 1000;
    for (int i = 0; i < taloes.size(); i++) {
        TalaoAIT talaoAIT = taloes.get(i);
        em.persist(talaoAIT);
        if(i % batchSize == 0) {
            em.flush();
            em.clear();
        }
        taloes.add(talaoAIT);
    }
    em.flush();
    em.clear();
    

    Next you shouldn't make your batches to large as that can cause memory problems, start with something like 50 and test which/what performs best. There is a point at which dirty-checking is going to take more time then flusing and clearing to the database. You want to find this sweet spot.

提交回复
热议问题