1

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?

piotrek
  • 13,982
  • 13
  • 79
  • 165
  • See http://stackoverflow.com/questions/30451470/connection-to-db-dies-after-424-in-spring-boot-jpa-hibernate (although it is about MySQL, the settings apply to other drivers as well). – M. Deinum Aug 04 '16 at 12:47

1 Answers1

3

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

piotrek
  • 13,982
  • 13
  • 79
  • 165