如果数据库连接失败,如何防止 Hikari 池初始化失败

How to prevent failure on Hikari Pool initialization if database connection fails

我有一个使用 Hikari 连接池的项目。我为所有数据库一一创建连接池 new HikariDataSource(someConfig)

当所有数据库都可用时,这工作正常。但是,如果任何数据库离线,Play 项目会出现以下错误:

[RuntimeException: java.lang.ExceptionInInitializerError]

如果只有少数数据库离线,我不希望应用程序崩溃。

如何避免此错误导致应用程序崩溃?

注意:我在另一个子项目中单独使用 HikariCP 包。我没有使用 play-hikari 插件。

设置 hikaricp 属性 initializationFailFast=false。它的描述来自 hikaricp site:

This property controls whether the pool will "fail fast" if the pool cannot be seeded with initial connections successfully. If you want your application to start even when the database is down/unavailable, set this property to false. Default: true