Wait for database connection in Spring Boot without an exception

Dav*_*nig 5 java spring exception mariadb spring-boot

I want to create a microservice with Spring Boot. For persistence i use a mariadb database. To wait for the database which is running in a docker container, i implemented the following code like shown here:

@Bean
    public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
        var dsv = new DatabaseStartupValidator();
        dsv.setDataSource(dataSource);
        dsv.setTimeout(60);
        dsv.setInterval(7);
        dsv.setValidationQuery(DatabaseDriver.MYSQL.getValidationQuery());
        return dsv;
    }
Run Code Online (Sandbox Code Playgroud)

The code is working very well, my application is now waiting for the database connection. But i get an exception at startup of the application:

java.sql.SQLNonTransientConnectionException: Could not connect to Host ....
...
...
...
Run Code Online (Sandbox Code Playgroud)

In the next line i get an information, that it will wait for the database:

021-04-07 21:29:40.816 INFO 16569 --- [ main] o.s.j.support.DatabaseStartupValidator : Database has not started up yet - retrying in 7 seconds (timeout in 57.65 seconds)

After that the application is starting as expected. So i think everything is working fine, but what i have to do to suppress the Exception? In the linked article it should work without an exception. Do i have to implement the "dependsOnPostProcessor" function? Which dependency i have to use? Sorry, possible a dumb question, i am new to spring boot.

Raf*_*ffa 2

要摆脱该异常,您可以在application.properties文件中声明以下指令:

logging.level.com.zaxxer.hikari=OFF
Run Code Online (Sandbox Code Playgroud)

请记住,如果应用程序无法与数据库联系,您的 spring 会在一段时间后由于该异常而崩溃。此外,上述指令会阻止您查看与 Hikari 相关的任何日志记录活动。

总之,您可以隐藏异常的外观,直到应用程序因超时而终止为止。

希望我能澄清一下情况