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
|