春季重试与交易 [英] Spring Retry with Transactional

查看:79
本文介绍了春季重试与交易的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Retry是否可以与Spring的@Transactional注释一起使用?

Is Spring Retry guaranteed to work with Spring's @Transactional annotation?

具体地说,我正在尝试使用@Retryable进行乐观锁定.似乎这取决于所创建的AOP代理的顺序.例如,如果调用看起来像这样:

Specifically, I'm trying to use @Retryable for optimistic locking. It seems like it would be dependent on the ordering of the AOP proxies that were created. For example, if the calls look like this:

呼叫代码->重试代理->事务代理->实际数据库代码

Calling code -> Retry Proxy -> Transaction Proxy -> Actual DB Code

然后它将正常工作,但是如果代理的结构如下:

Then it would work correctly, but if the proxies were structured like this:

呼叫代码->事务代理->重试代理->实际数据库代码

Calling code -> Transaction Proxy -> Retry Proxy -> Actual DB Code

然后重试将不起作用,因为关闭事务的行为就是抛出了乐观锁定异常的原因.

Then the retry wouldn't work, because the act of closing the transaction is what throws the optmistic locking exception.

在测试中,它似乎生成了第一种情况(重试,然后是事务),但是我无法确定这是保证的行为还是幸运的.

In testing, it appeared to generate the first case (retry, then transaction), but I couldn't tell if this was a guaranteed behavior or just lucky.

推荐答案

在这里找到了答案: https://docs.spring.io/spring/docs/5.0.6.BUILD-SNAPSHOT/spring-framework-reference/data-access.html#transaction-declarative-annotations 表2指出Transactional批注的建议的顺序为Ordered.LOWEST_PRECEDENCE,这意味着将RetryableTransactional组合在一起是安全的,只要您不覆盖任何一个的建议的顺序这些注释.换句话说,您可以安全地使用以下表单:

Found the answer here: https://docs.spring.io/spring/docs/5.0.6.BUILD-SNAPSHOT/spring-framework-reference/data-access.html#transaction-declarative-annotations Table 2 indicates that the advice for the Transactional annotation has an order of Ordered.LOWEST_PRECEDENCE, which means that it is safe to combine Retryable with Transactional as long as you aren't overriding the order of the advice for either of those annotations. In other words, you can safely use this form:

@Retryable(StaleStateException.class)
@Transactional
public void performDatabaseActions() {
    //Database updates here that may cause an optimistic locking failure 
    //when the transaction closes
}

这篇关于春季重试与交易的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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