JPA/Hibernate improve batch insert performance

后端 未结 2 1989
一整个雨季
一整个雨季 2021-01-07 04:57

I have a data model that has a ONE TO MANY relationship between ONE entity and 11 other entities. These 12 entities together represent one data packet. The problem I am havi

2条回答
  •  死守一世寂寞
    2021-01-07 05:36

    I have managed to solve this problem by using Hibernate Sessions for each 'group' of inserts. The results are about a 7-fold reduction in time needed to save the data. Used to take approximately 2000ms to save one 'packet' and now it takes between 200ms and 300ms to do the same thing.

    Just to repeat - this is valid for Play! Framework 1.2.3 - I am not sure whether, or how this applies to other frameworks or applications that utilize Hibernate.

        Session mySession = (Session) Pressure.em().getDelegate();
    
        for(int i = 0 ; i < data.size() ; i++){
            initializeFromJsonAndSave(data.get(i), mySession);
        }
        s.flush();
        s.clear();
    

    The 'initializeFromJsonAndSave' method was changed so that, instead of calling the object's save() method, calls mySession.save(myNewObject).

提交回复
热议问题