【问题标题】:Hystrix timeout doesn't rollback transaction in spring bootHystrix 超时不会在 Spring Boot 中回滚事务
【发布时间】:2019-03-30 15:57:09
【问题描述】:

我正在尝试在 Spring Boot 中的方法上使用 @Transactional 实现 hystrix。

@Transactional(transactionManager="primaryTrnsManager")
@HystrixCommand(commandKey ="createRecord", fallbackMethod="createRecordFallback", commandProperties={
@HystrixProperty(name="execution.siolation.thread.timeoutInMilliseconds",value="1000"),
@HystrixProperty(name="circuitBreaker.requestVoulumeThreshold",value="20"),
@HystrixProperty(name="circuitBreaker.sleepWindowInMilliseconds",value="5000"),
@HystrixProperty(name="circuitBreaker.errorThresholdPercentage",value="50")})
public String createRecord(String name){
...............
//Dbcall
}
@Transactional(transactionManager="backUptranManager",propagation=propagation.REQUIRES_NEW)
public String createRecordFallback(){
//dbcall

}

发生的事情是当 hystrix 超时发生时,我对数据库的调用没有回滚,hystrix 回退到辅助并再次使用相同的 sql 查询调用数据库。如果超时,我想中止前一个事务并开始另一个事务。通常@Transactional 会这样做,但使用 hystrix,我会插入重复的记录。

有什么帮助吗?

【问题讨论】:

    标签: spring-boot timeout transactional hystrix


    【解决方案1】:

    发生这种情况是因为 HistryxCommand 在完全不同的线程中运行,因此,当“Histryx 管理线程”(正在监视您的命令线程的线程)达到超时时,它会将命令线程标记为被中断,但是什么Hyxtrix 无法管理其中发生的事情。

    因此,达到您想要的行为的最佳方法是设置事务的超时时间,甚至在 jdbc 库级别,因此,超时将在命令线程内部进行管理,如果达到了, Timeout Exception 将从命令线程内部抛出,Hyxtrix 将对其进行妥善管理。

    【讨论】:

      猜你喜欢
      • 2020-03-11
      • 2018-11-05
      • 2020-02-06
      • 2016-10-07
      • 2017-11-07
      • 2018-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多