如何重新启动陷入僵局/锁超时交易春天? [英] How to restart transactions on deadlock/lock-timeout in Spring?

查看:188
本文介绍了如何重新启动陷入僵局/锁超时交易春天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是执行时死锁或锁定超时异常交易重新开始使用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:


  • 您的重试拦截应该是一定要解除绑定任何preexisting线装Hibernate会话,并重新绑定一个新的。一旦一个异常(例如,死锁)从休眠/ JDBC $ C $内抛出c中的相应Hibernate会话中毒,需要被丢弃。 ( session.clear()是不够的。)

  • 如果您的事务性服务的方法使用Hibernate的会话对象作为方法参数要小心。在重试,当您重置Hibernate会话,这些对象将被分离。你需要重新连接它们,如果服务方法假定它们连接(例如,如果他们使用获得的服务方法访问,或者如果你试图救他们,等延迟加载属性)一般情况下,它的更好,如果你不要使用Hibernate对象作为参数,以交易服务方法。

  • 您会实施 MethodInterceptor.invoke() - 这被传递给这个的MethodInvocation 实例可能是有状态的;你可能需要在拦截器使用前克隆。

  • Your "retry interceptor" should be sure to unbind any preexisting thread-bound Hibernate session and rebind a new one. Once an exception (e.g., deadlock) is thrown from within Hibernate/JDBC code the corresponding Hibernate session is poisoned and needs to be discarded. (session.clear() is not sufficient.)
  • Be careful if your transactional service methods use Hibernate session objects as method parameters. On retry, when you reset your Hibernate session, these objects will be detached. You'll need to reattach them if the service method assumes they are attached (e.g., if they use lazy loaded properties that get accessed in the service method, or if you try to save them, etc.) In general, it's better if you don't use Hibernate objects as parameters to transactional service methods.
  • You'll be implementing 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.

这篇关于如何重新启动陷入僵局/锁超时交易春天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆