I am developing an application that uses hibernation and save Entitiesas usual inside transactions hibernate. I want to “get feedback” from a transaction if it completed successfully or not, and accordingly to exclude the following code. Here is the trivial method that I use to update an object:
public boolean updateDepartment(Department s) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Transaction tx = HibernateUtil.getTransaction(session);
boolean success = false;
try
{
tx.begin();
session.update(s);
tx.commit();
success = true;
}
catch (Exception e)
{
tx.rollback();
e.printStackTrace();
success = false;
}
return success;
}
Calling a method from another code:
boolean b = dao.updateDepartment(d);
if(b)
{
doStuff();
}
else
{
showMessage("Save not usccessful. Try again");
}
My question is whether this approach with a variable is the booleanbest way or whether it can be done better. If my approach is ok, would it be better if the operator returnwere surrounded finally?
source
share