Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring, Morphia and DataAccessException implementation

I'm using Morphia and MongoDB with my Spring application. I see in many of the example projects that many of the service interface methods throw a DataAccessException. From what I can tell, this exception is thrown from various framework classes to simplify exception handling for various implementations of data access.

At this point I'm guessing I should catch any errors thrown by Morphia and throw a DataAccessException from my service implementation. So my question is, should I model this approach with my service implementations that use Morphia? Or perhaps I'm just misunderstanding this.

like image 930
Matt W Avatar asked Aug 22 '26 02:08

Matt W


1 Answers

This makes sense if you want your business logic to be able to react to specific types of DataAccessException, without being dependent on the Morphia/Mongo types.

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

However, if your business logic or exception-handling logic doesn't really care which exception type is thrown, then there would seem to be little point.

like image 108
skaffman Avatar answered Aug 23 '26 18:08

skaffman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!