I have a question regarding (Generic Data Access Objects)
http://www.hibernate.org/328.html
Specifically, the line about not forgetting exception handling:
// Plain JDBC: HibernateUtil.getCurrentSession().getTransaction().commit(); // Don't forget exception handling
I've implement the examples described in the URL above (everything works perfectly), but have yet to add exception handling.
How do I add hibernate exception handling to this block of code? Is it needed?
HibernateUtil.getSessionFactory().getCurrentSession().beginTransaction();
MemberInviteDAO memberInviteDAO = FACTORY.getMemberInviteDAO();
memberInviteDAO.makePersistent(memberInvite);
HibernateUtil.getSessionFactory().getCurrentSession().getTransaction().commit();
Do I need to catch a runtime exception as stated in the Hibernate API or is my code fine as above?
Session sess = factory.openSession();
Transaction tx;
try {
tx = sess.beginTransaction();
//do some work
...
tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
throw e;
}
finally {
sess.close();
}
Depending on the answers to above, if I'm not persisting objects to the database (only using load), do I need to wrap my code in a try/catch block?
Thanks,
Michael