Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

where to handle spring DataAccessException

I develop an application using Struts, Spring, and Hibernate.

My DAOs uses spring jdbc and all its method throws DataAccessException(that is uncheked).

Where should I handle this exceptions? I know it's an unchecked exception but I think I need to tell the user if there's a problem with the db or it's connection.

I think I should rethrow the DataAccessException from my service class methods to be catched by Controller. Is this a good practice?

I've looked at the samples from Spring package and didn't find any Exception handling in the domain or service area. The DataAccessException seems to be ignored after leaving dao area.

Please suggest a good design for this matter.

like image 601
rohit Avatar asked May 20 '12 11:05

rohit


People also ask

How do you handle DataAccessException?

you need not handle any database-related exceptions explicitly instead spring jdbc framework will handle it for you. all the exceptions thrown by the spring jdbc framework are subclasses of dataaccessexception which is a type of runtimeexception, so you need not handle it explicitly.

What is DataAccessException in spring?

Class DataAccessException This exception hierarchy aims to let user code find and handle the kind of error encountered without knowing the details of the particular data access API in use (e.g. JDBC). Thus, it is possible to react to an optimistic locking failure without knowing that JDBC is being used.

How do you handle exceptions in DAO layer in spring?

Every layer should have however their specific exceptions as generic. for example, DAO layer may have custom exception handlers like DavaSavingException, IOException etc.. So the approach is throw exception from DAO to service layer and again throw it to UI layer and catch in UI specific classes.

How do I instantiate DataAccessException?

DataAccessException is an abstract class and can not be instantiated. Instead use one of the concrete classes such as new DataRetreivalFailureException("this was the reason") or create your own: throw new DataAccessException("this was the reason") {}; And you get an anonymous class derived from the DataAccessException.


1 Answers

The DataAccessException seems to be ignored after leaving dao area.

And that's a good thing! Let it fly down through the whole stack. You probably have transactions on service layer - the exception will transparently cause the outermost transaction to be rolled-back. Great!

Now it will find its way to the controller. If you catch it in the Struts controller, you can e.g. return different view. But most likely you don't want to handle exception in each and every Struts action. So let the exception fly even further down. At some point Struts will catch that exception and try to handle it. Struts has some sophisticated error handling mechanisms, you'll find plenty of information about them. Typically it will invoke some custom action or error screen depending on exception type.

Finally, if even Struts can't handle the exception, it will be rethrown to the container, causing HTTP 503 with exception details being returned.

As you can see you can control exceptions on a lot of levels, typically lower is better.

like image 118
Tomasz Nurkiewicz Avatar answered Sep 19 '22 07:09

Tomasz Nurkiewicz