Spring, Morphia implementation and DataAccessException

I am using Morphia and MongoDB with my Spring application. In many examples, I see that many of the service interface methods throw a DataAccessException. From what I can say, this exception is thrown from different classes of infrastructure in order to simplify exception handling for various data access implementations.

At this point, I assume that I should catch any errors caused by Morphia and throw a DataAccessException from my service. So my question is: should I model this approach using my services that use Morphia? Or maybe I just don't get it.

+3
source share
1 answer

This makes sense if you want your business logic to be responsive to specific types DataAccessException, independent of Morphia / Mongo types.

The easiest way to do this is to write a class that implements PersistenceExceptionTranslatorand that knows how to translate Morphia / Mongo exceptions into DataAccessException. Declare this class as a bean, and Spring will automatically ask it to throw exceptions if your DAO class is annotated with @Repository.

However, if your business logic or exception handling logic really doesn't care about what type of exception will be thrown, then there seems to be little point.

+3
source

Source: https://habr.com/ru/post/1775002/


All Articles