spring-boot:数据库中断后的jdbc重新连接

时间:2016-08-04 10:57:49

标签: java spring postgresql jdbc spring-boot

我使用带有jdbc模板的spring-boot和datasource(postgres)。在我的服务中我做

@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

如何自动重新连接数据源?

1 个答案:

答案 0 :(得分:3)

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

spring:
  datasource:
    testOnBorrow: true
    validationQuery: select 1

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

相关问题