From edd362dcd1b36e98a957714dadfbdfcdd4c7bf58 Mon Sep 17 00:00:00 2001 From: Alex Osborne Date: Wed, 20 Sep 2023 22:50:23 +0900 Subject: [PATCH] Wait for database startup on boot --- common/resources/common.properties | 2 ++ .../pandas/core/DatabaseStartupConfig.java | 31 +++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 common/src/pandas/core/DatabaseStartupConfig.java diff --git a/common/resources/common.properties b/common/resources/common.properties index d0ba17c7..ac7fa90f 100644 --- a/common/resources/common.properties +++ b/common/resources/common.properties @@ -2,6 +2,7 @@ spring.datasource.url=${PANDAS_DB_URL:jdbc:h2:file:./data/db;AUTO_SERVER=true} spring.datasource.username=${PANDAS_DB_USER:pandas3} spring.datasource.password=${PANDAS_DB_PASSWORD:pandas3} spring.datasource.hikari.maximum-pool-size=15 +spring.datasource.hikari.initialization-fail-timeout=-1 spring.h2.console = false @@ -17,4 +18,5 @@ logging.level.org.hibernate.engine.transaction.jta.platform.internal.JtaPlatform logging.level.org.hibernate.jpa.internal.util.LogHelper=WARN logging.level.org.hibernate.orm.incubating=ERROR logging.level.org.springframework.data.repository.config.RepositoryConfigurationDelegate=WARN +logging.level.org.springframework.jdbc.support.DatabaseStartupValidator=debug logging.level.org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean=WARN \ No newline at end of file diff --git a/common/src/pandas/core/DatabaseStartupConfig.java b/common/src/pandas/core/DatabaseStartupConfig.java new file mode 100644 index 00000000..16b4bcb7 --- /dev/null +++ b/common/src/pandas/core/DatabaseStartupConfig.java @@ -0,0 +1,31 @@ +package pandas.core; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.orm.jpa.EntityManagerFactoryDependsOnPostProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.jdbc.support.DatabaseStartupValidator; + +import javax.sql.DataSource; +import java.util.concurrent.TimeUnit; + +@Configuration +public class DatabaseStartupConfig { + @Bean + public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) { + var validator = new DatabaseStartupValidator(); + validator.setDataSource(dataSource); + validator.setTimeout((int) TimeUnit.HOURS.toSeconds(4)); + return validator; + } + + /** + * This makes EntityManagerFactory depend on DatabaseStartupValidator to block startup + * until the database is ready. + */ + @Bean + public static EntityManagerFactoryDependsOnPostProcessor databaseStartupDependency() { + return new EntityManagerFactoryDependsOnPostProcessor("databaseStartupValidator"); + } +}