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.
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.
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.
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.
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