Back Forum Reply New

DataAccessException Usage ??

Hi All,

I have batch process that reads record by record from a table and performs calculations and then inserts back to another table. This is done in small chunks. For easy development, I am planning to use JDBCTemplate or HibernateTemplate at DAO layer and I am also planning to use DI and Spring transaction control. My design is interface based. Here are the layers: Calculations--gt;Service--gt; DAO.

In general, I may have two data access exception categories. One, the database may go down during the batch process; in this case I need to stop the overall batch process.  Two, I may have data related issue such as DataIntegrityViolation or DataTruncation, in this case I need to ignore the record and proceed to next record. Whether to stop the process or ignore the record is decided at calculation layer, means the exception should be propagated to all the way to calculations layer. For support reasons, I need to log the data that is causing the data issue at service layer, this I can do by catching the DataAccessException at service layer and then log the data. In both scenarios I will be using DataAccessException in all layers.

Here is my question:
Can I use spring DataAccessException directly in all layers?. Is this a good practice/design to spread a frame work class all over the application?. Down the line if we choose to go for different frame work would it be difficult to change it at that time.

Is that at a good idea to create application specific DataAccessException which internally extends Spring DataAccessException ?.

Please share your thoughts.

Thanks in advance,
Venkat

Can I use spring DataAccessException directly in all layers?. Is this a good practice/design to spread a frame work class all over the application?. Down the line if we choose to go for different frame work would it be difficult to change it at that time.
I don't think there is any problem in using it any layer to which the concept of a data access exception is relevant. It would not be too difficult to change if you used another framework because the DataAccessException hierarchy is not tied to the rest of Spring. And AFAIK no other framework has anything similarly sophisticated, so it would still probably add value in such a case.

Is that at a good idea to create application specific DataAccessException which internally extends Spring DataAccessException ?

Well, Java doesn't have the concept of quot;privatequot; inheritance like C++ so it's not possible to quot;internallyquot; extend and guaratee that callers won't rely on that.

I think this is a really bad idea. Why reinvent something that Spring provides that saves you a lot of time? The subclassing apporach just won't work as you would have to subclass many exceptions and would want to catch the higher-up classifications that Spring provides.

In general, minimizing dependencies on a framework is good practice and Spring goes a long way to achieving it. But in this case, I think you would simply create work for yourself and decrease value.

Rod,

Thanks for the information. I am new to Spring and just started realize how accessible the core team is.

Thanks, Venkat
¥
Back Forum Reply New