【问题标题】:Killing DB Query after timeout超时后终止数据库查询
【发布时间】:2022-11-18 06:14:03
【问题描述】:

[Java Spring-boot] 我有一个查询数据库的 API,我想这样做,如果查询在 5 秒后没有完成,就会抛出异常。我当前的代码如下所示。

@GetMapping("/return")
@Transactional(timeout = 5)
public List<TestEntity> findAll() throws InterruptedException {
    return testRepository.findAll();
}

然而,这段代码不会在 5 秒后超时,因为超时只会在每个步骤完成后触发,例如,如果 return testRepository.findAll(); 需要 15 秒才能执行,那么代码只会超时 15 秒(在那之后方法完成)。有没有更好的注释或方法允许我在任何方法中途抛出异常,这样即使我们在testRepository.findAll(); 中间,异常仍然会在 5 秒结束时抛出?

谢谢!

【问题讨论】:

    标签: java multithreading spring-boot exception timeout


    【解决方案1】:

    添加spring.datasource.hikari.connection-timeout= 5000applicaton.yml

    【讨论】:

      猜你喜欢
      • 2014-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-26
      • 2020-09-03
      • 1970-01-01
      • 2017-12-09
      相关资源
      最近更新 更多