0

I wanted to know how to gracefully handle all exceptions that can possibly arise due to db CRUD operations. In case of any exception, MongoException can be caught and analyzed what went wrong, it may be due to db server/ network/ time out issues . In the most recent version of Morphia, there is a way to catch these exceptions. But I needed info about expected exceptions that can arise for each of the below operations i.e best way to know if operation succeeded or failed.
- get/find
- merge
- update
- delete
- save.

Can someone suggest the best possible way to handle this? I seem to be lil stuck on this as docs dint help.

Thanks :)

1 Answers1

0

There are a lot of different ways that things can "break" when a client application is communicating with a database. Trying to sort out exactly what the problem is complicated, and how you want to handle these errors will really depend on the nature of your application and how robust your application needs to be.

In simple cases, if you get an error you can just try again. But it's not that easy with every operation. If you insert a document and get an error back, there's no way to tell for sure exactly what went wrong. So a simple solution is to try the insert again with the same key (_id).

There's a great blog post describing this in much better detail: How To Write Resilient MongoDB Applications.

helmy
  • 9,068
  • 3
  • 32
  • 31