0

When optimistic locking is enabled in Hibernate, Hibernate throws a runtime exception (HibernateOptimisticLockingFailureException).

When this exception is thrown we need to catch this exception and display a meaningful message to the user. (ex. "The object is modified by someone else.")

What I am thinking to do is catching this Runtime Exception in the service level and rethrow a custom checked exception like ConcurrentObjectModificationException.

Is this the correct approch to handle this?

hetptis
  • 786
  • 1
  • 12
  • 23
  • The idea sounds OK to me ([and to other people as well](http://stackoverflow.com/questions/8104407/cant-java-unchecked-exceptions-be-handled-using-try-catch-block)). Maybe include some code if you still have doubts. – Tim Biegeleisen Mar 01 '17 at 04:41
  • Thanks. I needed to get confirmed this approach is not a really bad idea. – hetptis Mar 01 '17 at 04:45
  • Just because they throw unchecked does not mean that someone would not have a reason to catch it. But perhaps you should question your app's logic regarding why it is even possible for this exception to happen under normal or semi normal circumstances. – Tim Biegeleisen Mar 01 '17 at 04:48
  • This happens when two users simultaneously open an Object, then one save first. Then other tries to save. Originally second save will overwrite the first save. But we are planning to enable hibernate optimistic locking to prevent users overwriting changes unknowingly. With optimistic locking, hiberate will throw a runtime exception on concurrent updates. – hetptis Mar 01 '17 at 04:52

0 Answers0