无法为Spring Boot JDBC创建重试数据源

发布于 2025-02-07 10:15:15 字数 1003 浏览 1 评论 0 原文

我想添加数据库连接的重试功能,直到该应用程序获取它为止。因为我在 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

我看到了调试日志,但这些错误并没有帮助。这是示例源代码。请

注意:我的应用需要构建中提到的依赖项。我只提取了重试部分。

I would like to add the retry feature for database connection certain number of times until the app acquires it. For the I have used the spring-retry on the DataSource but it is not working. It is throwing the following error

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

I have seen the debug logs but those are not helpful. Here is the sample source code . kindly help

Note: the dependencies mentioned in build.gradle is required for my app. I have only extracted the retry part.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

墨落画卷 2025-02-14 10:15:15

您的用例将延迟春季启动的开始,直到数据库启动为止。弹簧实际上是用一个可以做到这一点的组件的。 是该组件,自春季1.x以来就已经存在。

您可以将其添加为bean,它将等待进一步的引导程序,直到数据库启动为止。

@Bean
public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
    var dsv = new DatabaseStartupValidator();
    dsv.setDataSource(dataSource);
    return dsv;
}

有关更详细的说明,请参见我的博客文章

Your use-case is delaying the start of Spring Boot until your database is up. Spring actually ships with a component that does that. The DatabaseStartupValidator is that component and has existed since about Spring 1.x.

You can add it as a bean and it will wait for further bootstrapping until the database is up.

@Bean
public DatabaseStartupValidator databaseStartupValidator(DataSource dataSource) {
    var dsv = new DatabaseStartupValidator();
    dsv.setDataSource(dataSource);
    return dsv;
}

For a more detailed explanation see this blog post of mine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文