简体   繁体   中英

spring-boot:jdbc re-connection after db outage

i'm using spring-boot and datasource with jdbc template (postgres). in my service i do

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

and later 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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