title: How do I get Spring Boot to automatically reconnect to PostgreSQL?
body: <p>I am running Spring Boot connecting to a PostgreSQL database. I have verified that data is written to the database if Spring Boot is started after the database. </p>
  
  <pre><code>spring.datasource.url = jdbc:postgresql://localhost/dbname
  spring.datasource.username = user
  spring.datasource.password = secret
  spring.datasource.driver-class-name = org.postgresql.Driver
  spring.datasource.testOnBorrow=true
  spring.datasource.validationQuery=SELECT 1
  </code></pre>
  
  <p>When there are changes in the database in development, I run into exceptions: 
  <code>PreparedStatementCallback; SQL []; This connection has been closed.; nested exception is org.postgresql.util.PSQLException: This connection has been closed.</code></p>
  
  <p>Note that the database is accessible again when the exceptions occur; I think the problem is that connection is stale because the database endpoint it originally connected to is no longer available because of the restart. Restarting Spring Boot resolves the issue. </p>
  
  <p>How do I configure Spring Boot to reconnect to PostgreSQL so that I do not need to restart it? </p>
  
  <p>I have attempted to follow the answers in <a href="https://stackoverflow.com/questions/22684807/spring-boot-jpa-configuring-auto-reconnect">Spring Boot JPA - configuring auto reconnect</a> and <a href="https://stackoverflow.com/questions/38674583/how-to-reconnect-database-if-the-connection-closed-in-spring-jpa">How to reconnect database if the connection closed in spring jpa?</a>. I am not sure whether the PostgreSQL behavior is different from MySQL. My reading of the <a href="http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-sql.html" rel="noreferrer">Spring Boot documentation</a> has not helped; I do not know the components described well enough to understand what documentation I should be looking at. </p>