The following is basic structure of my transactions using hibernate.
Code:
public void createUser(User user)
throws HibernateException, LogConfigurationException
{
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Log log = LogFactory.getLog(Log4JLogger.class);
try
{
log.info("starting transaction");
session.beginTransaction();
log.info("saving user");
log.debug(user.toString());
session.save(user);
log.info("committing transaction");
session.getTransaction().commit();
}
catch(Exception e)
{
log.info("rolling back transaction");
session.getTransaction().rollback();
log.error(e.getMessage(), e);
throw new HibernateException(e);
}
}
If this is run in a web application environment, will getCurrentSession() always create new session? Or will it use an existing session which other users are utilizing?
Is it safe to use openSession instead of getCurrentSession() method of the SessionFactory? I'll appreciate any reply.
Kind regards,
Roy