-->
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: Auto retry using a Highly available DB
PostPosted: Fri Jun 06, 2008 10:06 am 
Beginner
Beginner

Joined: Mon Sep 26, 2005 8:22 am
Posts: 24
Hi everyone,
I have a little concern here.

I am using a DB which is configured as highly available. So, if the DB crashes, the datasource will point to a working system, quite intently.

But, the connection will be lost for a split moment, and the current transaction will hence fail.

So I would like to retry each and every operation if an exception occurs, and I don't really want to manually add an ugly hard coded loop to every DB operations.

I don't see any thing in Hibernate docs about something offering such a feature, but I may really be wrong.

Does any one sees a nice way of doing so?


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 06, 2008 11:48 am 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
What about subclassing the Hibernate Session and overriding the commit method with such code?

I haven't tried it, but it might be an idea? Then it's coded in one place that every class uses. Maybe even using aggregation instead of inheritance might be even better.

-Cameron McKenzie

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jun 09, 2008 4:30 am 
Beginner
Beginner

Joined: Mon Sep 26, 2005 8:22 am
Posts: 24
I guess you meant subclassing the Transaction, because the commit method is part of it.

But then I would have to create a TransactionFactory and a Transaction class. I don't really feel that comfortable with the Hibernate API.

Or I could look at the Session.flush() method, what do you think?

Maybe a FlushEventListener could do the trick, but I doubt it will be called if the flush fails.

Thanks a lot for your help!

Brice.


Top
 Profile  
 
 Post subject: Ok, sorted out
PostPosted: Mon Jun 16, 2008 9:08 am 
Beginner
Beginner

Joined: Mon Sep 26, 2005 8:22 am
Posts: 24
I guess I found an easy (but maybe crappy) way of doing it :

Code:
public class MyFlushEventListener extends DefaultFlushEventListener {
   private static final long serialVersionUID = -6356392955121838101L;

   private static final Log LOG = LogFactory.getLog(MyFlushEventListener.class);

   // will load it from a config file
   private static final int MAX_RETRY = 3;

   /**
    * @see org.hibernate.event.def.DefaultFlushEventListener#onFlush(org.hibernate.event.FlushEvent)
    */
   public void onFlush(FlushEvent event) throws HibernateException {
      int tryNo = 1;
      boolean doneCorrectly = false;

      while (tryNo <= MAX_RETRY && !doneCorrectly) {
         try {
            super.onFlush(event);
            doneCorrectly = true;
            tryNo++;
         } catch (Exception e) {
            StringBuffer buf = new StringBuffer("Can't flush the Hibernate session.");
            buf.append(MAX_RETRY - tryNo);
            buf.append(" try(s) to go.");
            LOG.warn(buf.toString(), e);
         }
      }
      if (!doneCorrectly) {
         StringBuffer buf = new StringBuffer("Couldn't flush the Hibernate session after ");
         buf.append(MAX_RETRY);
         buf.append(" try(s). Check WARN in the log.");
         String message = buf.toString();
         LOG.error(message);
         throw new HibernateException(message);
      }
   }
}


Does anyone have a advice about that code?

edit: thinking of it, there is still the Transaction opening problem. If I try to open a transaction and the DB is off line, I will be thrown away...


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jun 17, 2008 3:46 pm 
Expert
Expert

Joined: Tue May 13, 2008 3:42 pm
Posts: 919
Location: Toronto & Ajax Ontario www.hibernatemadeeasy.com
Well, if the DB is offline, there's really not too much you can do, is there? ;)

This seems like a workable solution.

_________________
Cameron McKenzie - Author of "Hibernate Made Easy" and "What is WebSphere?"
http://www.TheBookOnHibernate.com Check out my 'easy to follow' Hibernate & JPA Tutorials


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.