-->
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: Exception Wrapper => good or bad idea?
PostPosted: Mon Mar 15, 2010 6:06 am 
Newbie

Joined: Mon Mar 15, 2010 5:39 am
Posts: 2
Hi folks,

Although I used Hibernate in a few small projects, I think i have a very basic question. Our new Project should not be dependant on Hibernate, it should be easy to migrate for example to EclipseLink or another Persistance Framework. I usually use the DAO Pattern as described in the Book "Hibernate Made Easy" by Cameron McKenzie. (as described here).

My Question is now, if it is a good idea to write an Exception Wrapper for Hibernate Exceptions? I don't want to catch HibernateExceptions in my code, because it would be hard to migrate to other Persistence Frameworks. What I also don't like is the fact, that Hibernate throws only Unchecked Exceptions. I understand, that it makes sense (as described in the book"Java Persistence with Hibernate" by Christian Bauer on page 440), but i personally would want to force my team to handle all the Exceptions.

Now my idea: I'll write a general DatabaseException Class. And all Transaction calls would throw a this new Exception, if a Hibernate Exceptions occurs. This would look like this:

Code:
class HibernateUtil
{

.....

    public static void commitTransaction() throws MyDatabaseException
    {
        try
        {
        HibernateUtil.getSession().getTransaction().commit();
        }
        catch(Throwable t)
        {
            throw new MyDatabaseException(t);
        }
    }

    public static void rollbackTransaction() throws MyDatabaseException
    {
        try
        {
            HibernateUtil.getSession().getTransaction().rollback();
        }
        catch(Throwable t)
        {
            throw new MyDatabaseException(t);
        }
    }
}

.....



So if i call e.g. the rollback Method, I am forced to handle this exception. And I would get rid of the "standard Hibernate Exception Handling" like:

(Pseudocode)
Code:

try{
...
transaction.begintransaction();
(...)
transaction.commit();
}
catch (RuntimeException e){
try{
transaction.rollback();
} catch (RuntimeException ex {
}
throw e;
}
finally {
(...)
}


In other Threads most Users think you should not get rid of this kind of exception handling, but the problem is, I can not force programmers to catch Exceptions like this. What is your opinion?

Thanks so much,

Dominik


Top
 Profile  
 
 Post subject: Re: Exception Wrapper => good or bad idea?
PostPosted: Mon Mar 15, 2010 6:42 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
I think generally the idea of an Exception Wrapper is not a bad,
if you want transform Hibernates Unchecked Exceptions into checked exceptions.
But not in this way as you describe!
With this implementation

Code:
try
        {
        HibernateUtil.getSession().getTransaction().commit();
        }
        catch(Throwable t)
        {
            throw new MyDatabaseException(t);
        }
    }


you transform each Throwable into a MyDatabaseException, even if potentially the exception t has nothing to do with the database (for example a OutOfMemory Exception) !!

1. Never catch Throwable's in application code, this is very dangeroues.
On critical Exceptions the JVM could be in trouble and there it is important that the JVM can exit
without exception handling where further exceptions will raise.
The highest level for catching unchecked exceptions should always by RuntimeException.
2. Before wrapping a RuntimeException into MyDatabaseException I suggest you to examinate if the
RuntimeException has really to do with Hibernate or the Database.


Top
 Profile  
 
 Post subject: Re: Exception Wrapper => good or bad idea?
PostPosted: Mon Mar 15, 2010 7:39 am 
Newbie

Joined: Mon Mar 15, 2010 5:39 am
Posts: 2
Thanks for your reply, pb00067!

You are right, catching Throwables is not the best way. Thanks for the hint!

My approach now would be something like:

Code:
class HibernateUtil
{

.....

    public static void commitTransaction() throws MyDatabaseException
    {
        try
        {
        HibernateUtil.getSession().getTransaction().commit();
        }
        catch(HibernateException h)
        {
            throw new MyDatabaseException(t);
        }
    }

    public static void rollbackTransaction() throws MyDatabaseException
    {
        try
        {
            HibernateUtil.getSession().getTransaction().rollback();
        }
        catch(HibernateException h)
        {
            throw new MyDatabaseException(h);
        }
    }
}

.....


I'll give this a try. If anyone has another solution for this, I would love it if your share your thoughts :)

Greetings from Germany,

Dominik


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.