-->
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: flush before commit
PostPosted: Wed Jul 08, 2009 4:46 pm 
Newbie

Joined: Wed Jun 24, 2009 12:24 pm
Posts: 5
I'm new to Hibernate and have some very basic questions.

In the following piece of code...

Quote:
public Object hibernateMerge(Object object) throws DataAccessException {
ManagedSessionContext.bind(HibernateUtil.getSingletonSession());
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();

Object retval = HibernateUtil.getSessionFactory().getCurrentSession().merge(object);

HibernateUtil.getSessionFactory().getCurrentSession().flush();
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
ManagedSessionContext.unbind(HibernateUtil.getSessionFactory());
return retval;
}


...two questions:

(1) What does the flush() achieve right before the commit()? It is my understanding that the commit() also performs a flush() - so isn't the explicit flush() call redundant? Or worse, might cause any problems?

(2) It is obvious that HibernateUtil.getSessionFactory().getCurrentSession() in line 2 returns the same Session object as does HibernateUtil.getSingletonSession() in line 1. As such, is the ManagedSessionContext.bind necessary in this code?

In other words, can I replace the above code with the following?
Quote:
public Object hibernateMerge(Object object) throws DataAccessException {
Session session = HibernateUtil.getSingletonSession();
session.beginTransaction();
Object retval = session.merge(object);
session.getTransaction().commit();
return retval;
}


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.