These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 
Author Message
 Post subject: Bulk insertion problem in Hibernate
PostPosted: Tue Dec 11, 2007 6:02 am 
Newbie

Joined: Mon Dec 10, 2007 12:24 pm
Posts: 2
Hi,


i tried bulk insertion of 1000 records using Hibernate .

It took nearly 3 secs for 1000 records insertion.

i already set hibernate.jdbc.batch_size = 1000, still the performace is same.

When i checked the bulk insertion using jdbc , it took just 300MS to insert 1000 records.


Is there any other way to improve the performance of hibernate as if what jdbc does?.


FYI, i am using Hibernate 3.2.5, MySQL 5.0 and java 1.4

Thanks in advance.

nvseenu.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 6:55 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:40 am
Posts: 24
Hm, you may try to disable all sorts of caching, but honestly speaking, hibernate may not be the right tool for bulk data processing.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 7:08 am 
Newbie

Joined: Mon Dec 10, 2007 12:24 pm
Posts: 2
Thomson wrote:
Hm, you may try to disable all sorts of caching, but honestly speaking, hibernate may not be the right tool for bulk data processing.


Thanks Thomson.

even i tried by disabling second level cache , still the performance is same.


Is that happening because of Hibernate trying to get primary key from db and populate it into id field of persistent class during/after bulk insertion ?.

During bulk insertion using jdbc, i never keen on getting primary keys of inserted records and populating to id fields , mainly focus on whether they have been inserted successfully.


However, your suggestion is helpful for me.

So for bulk insertion , we have to depend on hand coded jdbc. am i right ?

Thanks,
nvseenu


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 11, 2007 7:27 am 
Beginner
Beginner

Joined: Wed Apr 26, 2006 4:40 am
Posts: 24
nvseenu wrote:
Is that happening because of Hibernate trying to get primary key from db and populate it into id field of persistent class during/after bulk insertion ?.


That is probably one of the reasons. (depends on what db you use) If you really want to dig deep into the problem, you could use profiling tools to check where hibernate/the data base burns most of the time during the process.

nvseenu wrote:
So for bulk insertion , we have to depend on hand coded jdbc. am i right ?

Yep, I would do. Hibernate is programmed very well (from what few I have seen from the source code), but nothing will beat straight forward hand written code that just fills prepared statements and executes them.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 4:28 am 
Regular
Regular

Joined: Fri May 12, 2006 4:05 am
Posts: 106
nvseenu wrote:
even i tried by disabling second level cache , still the performance is same.

The problem is not in using the second-level-cache, it's hibernate's first-level-cache aka persistence-context (pc).

hibernate will keep all of your 1000 inserted objects in the pc, so when doing mass-operations it's necessaryto clear the persistence-context in regular intervals (like every 50 inserts) via session.flush() and session.clear().

This will speed things up considerably, so hibernate could be used for the job. But overall Thomson is right, hibernate is not the best tool for these tasks.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 4:48 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
For bulk operations it is usually better to use the stateless session.
But then you dont have a lot of other features like dirty checking.

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 21, 2007 1:10 pm 
Beginner
Beginner

Joined: Wed May 16, 2007 7:12 am
Posts: 41
Location: London
Code:
StatelessSession session = sessionFactory.openStatelessSession();
Transaction tx = session.beginTransaction();
   
for ( int i=0; i<100000; i++ ) {
    Crap crap = new Crap(.....);
    session.save(crap);
}
   
tx.commit();
session.close();


ideally, don't do this via hibernate.

_________________
Dinesh Mahadevan

Visit me at http://www.yelani.com/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.