-->
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.  [ 1 post ] 
Author Message
 Post subject: suggestion needed on approach for bulk inserts
PostPosted: Tue Sep 29, 2009 11:57 am 
Regular
Regular

Joined: Fri May 22, 2009 4:50 am
Posts: 59
Hello,

I listen for an object which I recieve on rate of 5/sec. Which of the following approaches would be better:

1] where I open a session for each object recieved, save object in database and close the session

Sesion session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
session.save(obj);
tx.commit();
session.close();

2] where I listen for objects for certain period of time, open a session, iterate the list of recieved objects, save them and finally close the session.
i.e. I create only 1 session, for say 100 objects.

Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
for ( int i=0; i<list.size(); i++ ) {
Message obj= (Obj) list.get(i);
session.save(obj);
if ( i % 30 == 0 ) {
session.flush();
session.clear();
}
}
tx.commit();
session.close();

Is first approach correct to be used? Which one is better? as per my experiment I found 1st one faster.
Please share your suggestions and comments..

Thanks


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

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.