我想為資料庫連接添加重試功能一定次數,直到應用程式獲取它。對于我已經使用了 spring-retry ,DataSource但它不起作用。它拋出以下錯誤
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbcTemplate' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/JdbcTemplateConfiguration.class]: Unsatisfied dependency expressed through method 'jdbcTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: ExistingValue must be an instance of com.zaxxer.hikari.HikariDataSource
我已經看過除錯日志,但這些都沒有幫助。這是示例源代碼。好心的幫助
注意:我的應用程式需要 build.gradle 中提到的依賴項。我只提取了重試部分。
uj5u.com熱心網友回復:
您的用例是延遲 Spring Boot 的啟動,直到您的資料庫啟動。Spring 實際上附帶了一個執行此操作的組件。就是那個組件,DatabaseStartupValidator并且從 Spring 1.x 開始就已經存在。
您可以將其添加為 bean,它將等待進一步的引導,直到資料庫啟動。
@Bean
public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
var dsv = new DatabaseStartupValidator();
dsv.setDataSource(dataSource);
return dsv;
}
有關更詳細的說明,請參閱我的這篇博文。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/491624.html
標籤:春天 弹簧数据 spring-jdbc 春季重试
