在使用 Spring 时,在死锁或锁定超时异常时实现事务重启的最佳实践是什么(特别是 Spring 推荐的方法:声明性事务)?
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) ?
谢谢,
阿萨夫
我觉得 Spring 本身应该对这个问题有一个很好的答案(至少以文档的形式,或者某种重试拦截器).唉,它没有.
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.
可能处理重试的最佳方式(如果您想继续声明性"地处理事物)是编写您自己的拦截器实现,该实现将自动重试事务配置的次数.对于初学者,请学习 Spring 的 TransactionInterceptor
,它管理声明式事务的开始/回滚/提交行为.如果您使用的是 Hibernate,请注意它如何处理 Hibernate 会话绑定/取消绑定到当前线程.
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.
使用 Hibernate 时需要注意的事项:
Things to watch out for if you're using Hibernate:
session.clear()
是不够的.)MethodInterceptor.invoke()
-- 传入 this 的 MethodInvocation
实例可能是有状态的;在拦截器中使用它之前,您可能需要克隆它.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.这篇关于如何在 Spring 中重新启动死锁/锁定超时事务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!