MySql: No operations allowed after connection closed solution

Spring Boot data source configuration and No operations allowed after connection closed connection exception solution

Let me first talk about my project configuration: SpringBooot + SpringMVC+SpringData JPA+ MySql

There is nothing wrong with the early development and everything works well. But today I tested it again and reported the following error:

Pay attention to the key points when troubleshooting exceptions: No operations allowed after connection closed .

From this place we know that the mysql link has been closed. Of course, an exception occurs when accessing a closed link.

the reason:

The reason for this exception is that MySQL 5.0 has dealt with long-term DB connections afterwards, that is, if a DB connection has passed without any operation after 8 hours (Mysql server default "wait_timeout" Is 8 hours), Mysql will automatically close the connection. This is the problem. If the connections in the connection pool are idle for more than 8 hours, mysql will disconnect them, and the connection pool itself does not know that the connection has expired. If there is a Client requesting connection at this time, the connection pool will be invalid Connection provided to Client will cause the above exception.
Therefore, when configuring the datasource, you need to configure the corresponding connection pool parameters to check the validity of the connection and clean up invalid connections regularly.
 

What is the solution?

Add the following connection pool configuration under the configuration of the two data sources in application.properties:

#以下为连接池的相关参数配置
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
spring.datasource.validation-query=SELECT 1
spring.datasource.test-on-borrow=false
spring.datasource.test-while-idle=true
spring.datasource.time-between-eviction-runs-millis=18800

Supplement: The connection pool that spring boot will use by default is the tomcat connection pool, provided that the tomcat connection pool is available

 

Guess you like

Origin blog.csdn.net/qq_35393693/article/details/106984160