Question

I have used PROPAGATION_REQUIRED declarative transaction management approach for my Spring+Hibernate+Struts application. I can catch the StaleObjectException(intended) in the place where the service call is made (where the transaction starts). There are nested calls Foo1, Foo2, Foo3 in the service method which perform the updates. I want to catch the exception in these methods. How can I do so? Any alternatives or workarounds?

Was it helpful?

Solution

You shouldn't catch this exception. From the Hibernate reference documentation:

If the Session throws an exception, including any SQLException, immediately rollback the database transaction, call Session.close() and discard the Session instance. Certain methods of Session will not leave the session in a consistent state. No exception thrown by Hibernate can be treated as recoverable.

This means that other than catching the exception to transform it or wrap it into another kind of exception, catching the exception won't help you: using the session after won't work as expected anyway.

See How to deal with locks (JPA)? for another question where optimistic locking exception handling is discussed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top