How do I get Spring Boot to automatically reconnect to PostgreSQL?

后端 未结 1 1413
广开言路
广开言路 2021-02-07 07:51

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.



        
1条回答
  •  不思量自难忘°
    2021-02-07 08:11

    Spring boot should be configured to reconnect automatically, problem is that it is not aware of the broken connection.

    Since you are already using test on borrow and validation query, just try reducing validation interval so it is executed every time.

    spring.datasource.tomcat.test-on-borrow=true
    spring.datasource.tomcat.validation-query=SELECT 1
    spring.datasource.tomcat.validation-interval=0
    

    Tomcat jdbc connection pool on testOnBorrow:

    (boolean) The indication of whether objects will be validated before being borrowed from the pool. If the object fails to validate, it will be dropped from the pool, and we will attempt to borrow another. NOTE - for a true value to have any effect, the validationQuery or validatorClassName parameter must be set to a non-null string. In order to have a more efficient validation, see validationInterval. Default value is false

    But be aware of validationInterval:

    (long) avoid excess validation, only run validation at most at this frequency - time in milliseconds. If a connection is due for validation, but has been validated previously within this interval, it will not be validated again. The default value is 30000 (30 seconds).

    0 讨论(0)
提交回复
热议问题