I've just started learning Hibernate and I use the following pattern (from documentation) for every transaction:
private Session session;
private Transaction transaction;
protected List selectAll(Class clazz) throws HibernateException {
List objects = null;
try {
session = MyHibernateHelper.getSessionFactory().openSession();
transaction = session.beginTransaction();
// SELECT ALL
objects = session.createCriteria(clazz).list();
transaction.commit();
} catch (HibernateException exc) {
if (transaction != null) transaction.rollback();
throw exc;
} finally {
session.close();
}
return objects;
}
I can accept that every operation should be wrapped in a transaction. But it seems to me strange and unnecessary to rollback select
, in case it fails.
I think I can safely remove catch
block from the above example. And from any read-only operation. Am I right?