-->
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: Distinguishing database errors
PostPosted: Mon May 23, 2005 4:52 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
When a user deletes a record that has related child record a constraint is violated and I get a hibernate error.

All is good. Except that I want to handle this specific error differently than other database errors. I can see the specific Oracle error number in the text of the "cause" property of the hibernate exception.

Other than parsing for this, is there a way to set up error handlers for different database errors?

Lee


Top
 Profile  
 
 Post subject: Work around
PostPosted: Wed May 25, 2005 11:11 am 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
Well...no one came forward with a "hibernate kosher" way of handling this...so I just parsed the "cause" string.

For the benefit of others...here's my makeTransient method:

Code:
   
public void makeTransient(Metric metric) throws Exception
   {
      try {
         HibernateUtil.getSession().delete(metric);
         HibernateUtil.getSession().flush();
      } catch (HibernateException ex) {
         HibernateUtil.rollbackTransaction();
         if (parseException.isIntegrityConstraintViolation(ex)) {
            log.info("Integrity constraint violated");
            throw new ChildRecordsExistException(ex);
         } else {
            log.info("no integrity constraint violated");
            throw new InfrastructureException(ex);
         }
      }
   }


I created a class called "parseException" to have a set of static methods to perform operations on the exceptions. So far it just has the method used above, that determines whether the exception is caused by an oracle constraint violation.

Code:
   /**
    * Returns true if Oracle error ORA-02292 is part of the cause passed in
         * Constants.INTEGRITY_CONSTRAINT_VIOLATION = ORA-02292
    * @param cause
    * @return
    */
   public static boolean isIntegrityConstraintViolation(HibernateException ex) {
      boolean value = false;
      String cause = ex.getCause().toString();
      if (cause.indexOf(Constants.INTEGRITY_CONSTRAINT_VIOLATION) > 0) {
         return true;         
      } else {
         return false;
      }      
   }


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 11:57 am 
Newbie

Joined: Fri May 20, 2005 6:44 am
Posts: 1
I'm a new Hibernate user, but I think there is a better way to get the type of error produced.

There's a subclass of HibernateException called JDBCException, and this class has a method called getErrorCode() that works same way than SQLException.getErrorCode(), I mean it catches the underlying SQLException and return that errorCode.

Hope this helps.

Enric


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 12:02 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Also Hibernate3 has constraint-violation-specific subclasses of JDBCException.


Top
 Profile  
 
 Post subject:
PostPosted: Wed May 25, 2005 2:33 pm 
Beginner
Beginner

Joined: Tue Nov 02, 2004 1:34 pm
Posts: 45
enric.garre wrote:
There's a subclass of HibernateException called JDBCException, and this class has a method called getErrorCode() that works same way than SQLException.getErrorCode(), I mean it catches the underlying SQLException and return that errorCode.

Hope this helps.



Yep...that did the trick. I figured if I put up my bad code, someone would straighten me out :)

Lee


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.