Skip to content

Commit

Permalink
Wait for database startup on boot
Browse files Browse the repository at this point in the history
  • Loading branch information
ato committed Sep 20, 2023
1 parent 49460c4 commit edd362d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/resources/common.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
31 changes: 31 additions & 0 deletions common/src/pandas/core/DatabaseStartupConfig.java
Original file line number Diff line number Diff line change
@@ -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");
}
}

0 comments on commit edd362d

Please sign in to comment.