-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: where to properly catch HibernateException:struts/hibernate
PostPosted: Thu Aug 26, 2004 4:58 pm 
Beginner
Beginner

Joined: Sun Aug 22, 2004 11:00 am
Posts: 21
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?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 3:08 am 
Regular
Regular

Joined: Thu Aug 05, 2004 2:27 am
Posts: 54
Location: South Africa
Quote:
how else can implement this?

you could catch the HibernateException and throw a DAOException, or whatever you want to call it.
if you switch from hibernate, you'll always have a DAO, so everything else remains the same.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 27, 2004 1:52 pm 
Beginner
Beginner

Joined: Sun Aug 22, 2004 11:00 am
Posts: 21
perfect..thanks

_________________
Ive got a Tomcat that Struts then Springs then Hibernates


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.