What is the best practice on implementing a transaction restart upon deadlock or lock timeout exceptions when using Spring (specifically the Spring recommended approach: declarative transactions) ?
Thanks,
Asaf
I feel like Spring itself should have a good answer to this question (in the form of documentation, at the least, or a retry interceptor of some sort). Alas, it does not.
Probably the best way to handle retries (if you want to continue being "declarative" about things) is to write your own interceptor implementation that will automatically retry the transaction a configured number of times. For starters, study Spring's TransactionInterceptor
, which manages begin/rollback/commit behavior for declarative transactions. If you're using Hibernate, note how it handles Hibernate session binding/unbinding to the current Thread.
Things to watch out for if you're using Hibernate:
session.clear()
is not sufficient.)MethodInterceptor.invoke()
-- the MethodInvocation
instance that gets passed in to this may be stateful; you may need to clone it before using it in the interceptor.