-->
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.  [ 5 posts ] 
Author Message
 Post subject: Handling Hibernate Exception in Dao classes - Any strategy ?
PostPosted: Wed Sep 22, 2010 1:54 pm 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Hello,

I have coded my Dao with a generic class, and even a DaoException class, but I'm still wondering if I'm doing the right thing, and how to handle exceptions, that is, how to react to an exception after it occurred.

I was told, in case an Hibernate Exception is fired up, to roll back the transaction, close the session, and reattach the object to a new session... Now, this is very theoritical to me.

As to rolling back the transaction, is it something I need to do myself ? I'm asking because I'm running it with Spring that takes care of the transaction.

The Spring Hibernate configuration
Code:
   <bean id="transactionManager"
      class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory">
         <ref local="sessionFactory" />
      </property>
   </bean>

   <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
      <property name="sessionFactory">
         <ref bean="sessionFactory" />
      </property>
   </bean>


Here is chunks of my code to give an idea on what I stand:

Some of the generic Dao methods
Code:
   @Override
   public T makePersistent(T entity) {
      try {
         getSession().saveOrUpdate(entity);
      } catch (HibernateException e) {
         throw new DaoException("Saving the entity failed", e);
      }
      return entity;
   }

   @Override
   public void makeTransient(T entity) {
      try {
         getSession().delete(entity);
      } catch (HibernateException e) {
         throw new DaoException("Deleting the entity failed", e);
      }
   }


The DaoException
Code:
public class DaoException extends HibernateException {

   private Exception hiddenException;

   public DaoException(String error, Exception exception) {
      super(error);
      hiddenException = exception;
   }

   public Exception getHiddenException() {
      return (hiddenException);
   }
   
}


A Dao method
Code:
   @Override
   public void delete(Contact contact) {
      try {
         makeTransient(contact);
      } catch (ConstraintViolationException e) {
         // rollback and close the current session, start a new session and
         // make sure that the contact
         // is attached to the new session
      } finally {
         close();
      }
   }


And in there, you can see in comment what I'm trying to do..

I need it for a test that checks the well functioning of a Hibernate constraint violation exception.

The test
Code:
   @Test
   public void testDelete() {
      contactReferer0 = contactRefererDao.makePersistent(contactReferer0);
      contactReferer1 = contactRefererDao.makePersistent(contactReferer1);
      long count = contactRefererDao.countAllRows();
      assertEquals(2, count);
      contactRefererDao.makeTransient(contactReferer0);
      count = contactRefererDao.countAllRows();
      assertEquals(1, count);
      Contact contact = new Contact();
      contact.setEmail("email");
      contact.setMessage("message");
      DateTime contactDateTime = new DateTime();
      contact.setContactDateTime(contactDateTime);
      contact.setContactReferer(contactReferer1);
      contact = contactDao.makePersistent(contact);
      try {
         contactDao.delete(contact);
         fail();
      } catch (DaoException e) {
         // how to create a new session and reattach the contact ?
         // so as to be able to run the next contactRefererDao.countAllRows(); statement
      } finally {
      }
      count = contactRefererDao.countAllRows();
      assertEquals(1, count);
      contact.setContactReferer(null);
      contactRefererDao.makeTransient(contactReferer1);
      count = contactRefererDao.countAllRows();
      assertEquals(0, count);
   }


All my application compiles fine, all my tests pass fine too, except for the one above.

Tough this Hibernate cookie..


Top
 Profile  
 
 Post subject: Re: Handling Hibernate Exception in Dao classes - Any strategy ?
PostPosted: Wed Sep 22, 2010 2:41 pm 
Regular
Regular

Joined: Sun Feb 14, 2010 3:29 pm
Posts: 58
Location: USA
Read user manual on "Chapter 12. Transactions and Concurrency"

Couple points to noted are:

1) When a transaction failed, the transaction manager ought to automatically rollback and close your session. If you are using Spring for this, read their doc on how to set this up.

2) HibernateException is unchecked, but looks like you are adding your own level of checked exception? They got rid of it for a reason, because if you get them, they are likely unrecoverable. Again if you use Spring, they got another layer of so call "uniformed" exception handling, which you can add interceptors to handle these data exception.

If you are overwelmed with these, then I suggest read the manual little more carefully, and try out the Hibernate without any container first. Understand the basic of how Hibernate Session works, then you can take advantage of other container services that provided for you.

Hope these helps.

_________________
Zemian Deng
------------
Need a Java Scheduler? Try
http://bitbucket.org/timemachine/scheduler


Top
 Profile  
 
 Post subject: Re: Handling Hibernate Exception in Dao classes - Any strategy ?
PostPosted: Thu Sep 23, 2010 6:12 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
Thanks, I shall read the doc more thouroughlly.


Top
 Profile  
 
 Post subject: Re: Handling Hibernate Exception in Dao classes - Any strategy ?
PostPosted: Thu Sep 23, 2010 6:21 am 
Pro
Pro

Joined: Mon Apr 16, 2007 8:10 am
Posts: 246
After reading the documentation, I posted some follow up on the Spring forum this time.

http://forum.springsource.org/showthread.php?t=95674

http://forum.springsource.org/showthread.php?t=95675

Cheers


Top
 Profile  
 
 Post subject: Re: Handling Hibernate Exception in Dao classes - Any strategy ?
PostPosted: Thu Sep 23, 2010 8:03 am 
Newbie

Joined: Thu Sep 23, 2010 4:43 am
Posts: 10
hi,

i suggest you to use aop for handling transactions, define your service pointcut,(if you need exceptional advises , eclude them from scope)
define your session Factory (org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean) with your datasource,
inject your session factory with your org.springframework.orm.hibernate3.HibernateTransactionManager bean..

this way exception handling will be more proper and clear.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 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.