Skip to content

Commit

Permalink
Add DatabaseStartupValidator to wait for db to be available on boot
Browse files Browse the repository at this point in the history
This is to make the app resilient in the case where the app boots before the database is available.
  • Loading branch information
ato committed May 7, 2024
1 parent d0c5cba commit ac2edf7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions ui/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ logging.level.org.apache.tika.parser=error
logging.level.org.apache.fontbox=error
logging.level.org.apache.pdfbox=error
logging.level.org.vibur=warn
logging.level.org.springframework.jdbc.support.DatabaseStartupValidator=debug

# Management actuators
management.endpoints.web.exposure.include=loggers
Expand Down
29 changes: 29 additions & 0 deletions ui/src/bamboo/config/DatabaseStartupConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package bamboo.config;

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.DAYS.toSeconds(7));
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 ac2edf7

Please sign in to comment.