In our J2EE application, should we everytime we perform a hibernate request close the session? For instance, should we close it in the finally block, like in this code:
Code:
public KategoriData getKategoriById(String id) throws BaatkatalogenException {
if (id == null)
throw new BaatkatalogenException("id er null");
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Criteria criteria = session.createCriteria(KategoriData.class);
criteria.add(Expression.eq("id", id));
criteria.setMaxResults(1);
try {
return (KategoriData) criteria.uniqueResult();
} catch (HibernateException e) {
throw new BaatkatalogenException ;
} finally {
if(session.isOpen()) {
session.close();
}
}
}
If not, then when should we close it?
Because I dont know when the user is finished using the application. I am using c3p0, and I have defined idle time period and timeout, will that result in closing the sessions when the idle time is finished?