Hibernate saveOrUpdate large data

孤者浪人 提交于 2020-01-01 19:22:23

问题


I am trying to insert or update a large data with Hibernate. I have a list contains 350k objects and when I use Hibernate saveOrUpdate(), it takes hours to insert all the data.

I am using the code below for this operation. My development environment is JDK1.4 and Oracle Database.

public void saveAll(List list)throws HibernateException{
    Session session = HibernateUtil.getEYSSession();
    Iterator it = list.iterator();
    int i = 0;
    while(it.hasNext()){ 
        i++;
        Object obj = it.next();
        session.saveOrUpdate(obj);
        if (i % 50 == 0) { session.flush(); session.clear(); }
    }
}

I am using batch update and also set hibernate.jdbc.batch_size property 50 but it didn't help.

My object has one-to-one relation with another object so in this case using StatelessSession might be a problem. Because StatelessSession does not cascade to composed objects I think.

Do you have any ideas about how to increase the performance of saveOrUpdate() operation in this case?

Thanks.


回答1:


For every saveOrUpdate operation Hibernate generates a select statement as well. In my opinion updating or inserting huge data using Hibernate's saveOrUpdate is not at all a good option. I would suggest using either a stored procedure or move to SpringJDBCTemplate.




回答2:


I found that separating the instances where the save() method or the Hibernate merge() had to be used provided slightly better performance. But, I remember saving and updating 100,000 objects and it also took quite a long time. For that reason a stored procedure may be the more efficient mechanism.




回答3:


You can use futureTask to complete your database inserting. and continue your program.

This will not reduce the time but program doesn't wait for completing insert process.




回答4:


  Session session = sessionFactory.openStatelessSession();
  Transaction tx = session.beginTransaction();
  int i = 0;
  while(it.hasNext()){ 
    i++;
    Object obj = it.next();
    session.save(obj) 
    if (i % 50 == 0) { session.flush(); session.clear();}

statelessSession will help to increase performance.



来源:https://stackoverflow.com/questions/17083284/hibernate-saveorupdate-large-data

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!