This article mainly introduces how Spring Boot solves the Mysql disconnection problem. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
During the process of connecting Spring Boot JPA to Mysql, you will find a disconnection after 8 hours. The application.properties configuration is as follows (I skip this pit, welcome to join):
spring.datasource.url=jdbc:mysql://localhost/test spring.datasource.username=dbuser spring.datasource.password=dbpass spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Cause analysis:
Mysql by default, if a connection is found If the idle time exceeds 8 hours, the connection will be automatically closed on the database side. (mysql wait_timeout is 8 hours).
Solution:
1. Before Mysql 5 version, you can add autoReconnect=true after the URL, such as:
spring.datasource.url=jdbc:mysql://localhost/test?autoReconnect=true
2 . Add:
spring.datasource.test-on-borrow=false spring.datasource.test-while-idle=true spring.datasource.time-between-eviction-runs-millis= 3600000
3 to the application.properties file. Modify the wait_timeout time directly and crudely:
show global variables like 'wait_timeout';
Recommend the second method
The above is the detailed content of Detailed introduction on how to use Spring Boot to solve Mysql disconnection problem. For more information, please follow other related articles on the PHP Chinese website!