简体   繁体   English

spring-boot:db中断后jdbc重新连接

[英]spring-boot:jdbc re-connection after db outage

i'm using spring-boot and datasource with jdbc template (postgres). 我正在使用带有jdbc模板的spring-boot和datasource(postgres)。 in my service i do 在我的服务中我做

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

and later jdbcTemplate().query(...); 以后jdbcTemplate().query(...); . all works as expected but when i restart db, it stops working. 一切都按预期工作,但当我重新启动数据库时,它停止工作。 looks like datasource doesn't reconnect. 看起来像数据源不重新连接。 i got: 我有:

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

i got same error when i add: 我添加时出现同样的错误:

spring:
  datasource:
    testOnBorrow: true
    validationQuery: select 1

how to make datasource reconnect automatically? 如何自动重新连接数据源?

actually i found it does reconnect after adding: 实际上我发现它添加后重新连接:

spring:
  datasource:
    testOnBorrow: true
    validationQuery: select 1

i just needed to wait the default validationInterval that is 30sec 我只需要等待30秒的默认validationInterval

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM