-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DatabaseStartupValidator to wait for db to be available on boot
This is to make the app resilient in the case where the app boots before the database is available.
- Loading branch information
Showing
2 changed files
with
30 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |