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?
Asked
Active
Viewed 1,487 times
0

TJ-
- 14,085
- 12
- 59
- 90
1 Answers
4
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.
-
Well, I agree with you. But I want to kind of retry a part of the transaction. Do you think that's not a good idea? Should I repeat the whole process? [That could be a hit on the performance as seen by the user] – TJ- Aug 26 '11 at 14:01
-
You can't. If you want to retry (which is a bad idea in most of the cases, particularly if it's a a use-case involving an end user), you'll have to rollback the transaction and restart a new one, with a new Hibernate session. – JB Nizet Aug 26 '11 at 14:04
-
Can I wrap it/Intercept and wrap it so that I will be able to at least return custom exceptions to the caller (struts action class)? – TJ- Aug 26 '11 at 15:42
-
Of course you can. It's just a very bad idea to try continuing with the session after such an exception has been thrown by Hibernate. Make sure the transaction is rolled back. – JB Nizet Aug 26 '11 at 16:12
-
Is there a recommended way of doing that? – TJ- Aug 26 '11 at 16:48
-
A transaction is rolled back if any RuntimeException occurs when calling a transactional method. This default behavior can be altered by the rollbackFor and other attributes of the Transactional annotation (see http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/transaction/annotation/Transactional.html). You may also set the rollbackOnly flag manually with TransactionAspectSupport.getTransactionStatus().setRollbackOnly(). – JB Nizet Aug 26 '11 at 16:56