【问题标题】:spring-boot:jdbc re-connection after db outagespring-boot:数据库中断后jdbc重新连接
【发布时间】:2016-12-10 11:10:49
【问题描述】:

我正在使用带有 jdbc 模板 (postgres) 的 spring-boot 和数据源。在我的服务中,我愿意

@Autowired
public MyDao(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

及以后的jdbcTemplate().query(...);。一切都按预期工作,但是当我重新启动数据库时,它停止工作。看起来数据源没有重新连接。我得到了:

org.postgresql.util.PSQLException: This connection has been closed.

我在添加时遇到了同样的错误:

spring:
  datasource:
    testOnBorrow: true
    validationQuery: select 1

如何让数据源自动重连?

【问题讨论】:

标签: java spring postgresql jdbc spring-boot


【解决方案1】:

实际上我发现它在添加后确实重新连接:

spring:
  datasource:
    testOnBorrow: true
    validationQuery: select 1

我只需要等待默认的validationInterval 30 秒

【讨论】:

  • 您能否接受您的回答,以免问题显示为未回答?谢谢!
猜你喜欢
  • 2020-03-13
  • 2021-09-11
  • 1970-01-01
  • 2021-11-05
  • 2016-12-20
  • 2012-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多