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