Hibernate version:
2.1
i am developing an application using Struts and Hibernate. im having trouble deciding where to catch HibernateExceptions while abiding by the DAO pattern.
in a struts action i have the following code:
Code:
DAOFactory daoFactory = (DAOFactory)servlet.getServletContext().getAttribute(Constants.DAO_FACTORY_KEY);
UserDAO userDAO = daoFactory.createUserDAO();
boolean ok = userDAO.saveOrUpdate(user /*type: User*/);
then in the UserDAO class i have the following definition:
Code:
public boolean saveOrUpdate(User user) throws HibernateException{
//return true if save/update ok, false otherwise
Session session = sessions.openSession();
Transaction tx = null;
boolean status = false;
try{
tx = session.beginTransaction();
session.saveOrUpdate(user);
tx.commit();
status = true;
}
catch(HibernateException he){
if( tx != null )
tx.rollback();
//log he
}
finally{
session.close();
return status; //and i know i have a problem here if close() throws an exception
}
}
is declaring this method to throw a HibernateException a violation of the DAO pattern? if i do it this way i have to catch the HibernateException in the Action class...this would require one to change code in the Action class if i changed the DAO methods. how else can implement this?