1

I have a SpringBoot application which connects to mssql database. The SpringBoot application fails to start if database is not accessible (e.g database is down) at the time of startup. Is there any chance (configuration or "hack") to let the application start and build the datasource connection later when database is up again?

I use JDBCTemple instead of Hibernate and I create the DataSource bean as following:

@Bean
public DataSource carinDataSource() {
    final String pwdFromProperty = new ConfigReaderImpl().<String>readConfig(
        "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\MyApp", "DBMSPassword",
        env.getProperty("spring.carin-datasource.password"), String.class);

    String dbUserName = new ConfigReaderImpl().<String>readConfig(
        "HKEY_LOCAL_MACHINE\\SOFTWARE\\WOW6432Node\\MyApp", "DBMSLogin",
        env.getProperty("spring.carin-datasource.username"), String.class);

    DataSource dataSource = DataSourceBuilder.create().password(pwdFromProperty).username(dbUserName)
        .url(env.getProperty("spring.carin-datasource.jdbc-url"))
        .driverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver").
        build();


    return dataSource;
  }

I tried settings described in How to start spring-boot app without depending on Database? but did not help. But my intent is the same as described there.

  • 1
    Does this answer your question? [How to start spring-boot app without depending on Database?](https://stackoverflow.com/questions/23850585/how-to-start-spring-boot-app-without-depending-on-database) – Bessem Manita Mar 23 '22 at 09:07

0 Answers0