gpt4 book ai didi

java - 尝试连接数据库失败后不要关闭应用程序 java.net.ConnectException 数据库 : Connection refused

转载 作者:行者123 更新时间:2023-11-30 02:13:44 24 4
gpt4 key购买 nike

Spring Boot 数据/Postgres。

问题:如果应用程序启动时资源数据库 (postgres) 不可用,则应用程序将失败并出现错误。

Caused by: org.postgresql.util.PSQLException: Connection to localhost:5432 refused. Check that the hostname and port are correct and that the postmaster is accepting TCP/IP connections.
...
Caused by: java.net.ConnectException: Connection refused (Connection refused)

问题:如何让应用程序自动尝试重新连接?

假设:使用 Spring-Retry 项目通过重试尝试包装 @entityManagerFactory bean。

最佳答案

您尝试过处理异常吗?

尝试处理此异常,一旦发生,应用程序可以重新尝试连接到数据库。当然,让用户有机会决定是否希望重试以避免无限循环是有益的。

@KDecker,谢谢您的有效观点!

关于java - 尝试连接数据库失败后不要关闭应用程序 java.net.ConnectException 数据库 : Connection refused,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49284892/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com