The database connection pool connection fails, and an error is reported the last package......, wait_timeout defaults to 8 hours

Tip : This is my personal IT resource website, you can go in and have a look

During this period of time, I encountered a problem, the program reported the last package...
This problem is essentially a database connection pool problem. If you go deeper, it is that the TCP connection fails and the connection is unavailable. Here you need to understand the meaning of the following terms. The first is the connection pool. The connection pool maintains a group of jdbc connections. These connections may be used by threads at a certain time when the system is running, and some connections are idle. Of course, here is also There is a state transition, an idle connection will be transformed into an occupied state, and an occupied state will also be transformed into an idle state. Among them, the transition from the occupied state to the idle state requires passivation, that is, initialization. At the same time, passivation will also check whether the state of the connection can still be used, and if the connection is found to be abnormal, the connection will be destroyed. The nature of the jdbc connection is a TCP connection. When will this connection be broken? This is a probabilistic question, but generally we know that if mysql thinks that the connection has not been used for a long time, the server will actively disconnect the connection. If proxy is used, it will also follow the policy. Close the connection. However, it should be noted that TCP connections work in two directions. Unilaterally closing the connection will not cause the TCP connection to be closed. That is to say, the server closes the connection but the TCP connection is still there. This is very troublesome, which leads to the connection maintained in the connection pool. There will be unhealthy connections, that is, the connection is still unavailable. At this time, we can take out an unhealthy connection from the connection pool to send a request, but once the client calls the read method and wants to read the data returned by the server, an exception will be thrown. This is the root cause of the exception that you returned above. The failure time of the connection is determined by the two mysql parameters wait_timeout (non-interactive connection failure time) and interactive_timeout (interactive connection failure time). The default is 8 hours. 28800 seconds.
Solution
1. Modify the my.cnf file of mysql, add wait_timeout and interactive_timeout under mysqld to increase its invalidation time, but this approach only prolongs the time for the error to appear, and does not fundamentally solve the problem.
2. To deal with in the program, the database connection pool generally has related settings. For example, the c3p0 data pool I use. The connection in the connection pool can be checked regularly by setting the value of idleConnectionTestPeriod. It can be set to check once every 60s, and other connection pools There should also be related settings, and you can find and set them in a targeted manner.
Insert picture description here
This is one of the learning websites that I think is good. It is quite comprehensive. If everyone can finish the study, it is guaranteed to find a good job. Click here to check it out!

Guess you like

Origin blog.csdn.net/weixin_45345374/article/details/109115024