Spring JpaRepository findAll 方法执行返回空/无数据并且在执行时数据库连接失败时不抛出异常

问题描述 投票:0回答:1

我在生产 spring boot 应用程序中遇到问题。我在下面粘贴了示例代码:

所以在我们的例子中,数据存在于数据库中,下面的代码(findAll() 方法)是否有可能返回一个空列表而不是抛出异常,因为在执行代码时存在间歇性的应用程序和数据库连接失败

try{
List<POJO> list = repo.findAll();
}catch (Exception e){
    Log.error(e);
}

repo 在这里扩展了 JpaRepository。

java spring spring-data-jpa spring-data
1个回答
0
投票

只需从您的 catch 块返回一个空列表:

catch (Exception e){
  Log.error(e);
  return new ArrayList<POJO>();
}
© www.soinside.com 2019 - 2024. All rights reserved.