Solve the problem that mysql automatically disconnects all connections every 8 hours when the database connection pool connects to mysql

Solve the problem that mysql automatically disconnects all connections every 8 hours when the database connection pool connects to MySQL

There is a very annoying problem recently. In our project, we use our own connection pool to connect to the mysql database, but the mysql database will automatically disconnect all links every 8 hours, and the connection pool will be invalid. It needs to restart tomcat to be effective, huh, the server You can't always use " artificial intelligence " to intervene. Later, I flipped through the mysql manual and found that mysql has a solution. The following is the simplest solution:

When connecting to the database, add the parameter autoReconnect=true:

jdbc:mysql://localhost:3306/accounant?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true

However, there is this passage in the mysql manual: 
Should the driver try to establish a stale and/or dead connection again? If allowed, the driver will throw an exception for queries issued on a stale or dead connection (belonging to the current transaction), but will attempt to reconnect when the next query is issued on a connection of a new transaction. This feature is deprecated because it can have side effects related to session state and data consistency when the application does not handle SQLExceptions properly, and it is designed only for situations where you cannot configure application to properly handle SQLExceptions caused by dead and/or invalid connections. As an alternative, the MySQL server variable "wait_timeout" can be set to a higher value than the default of 8 hours. 
Oh, I don't know what the consequences of this "side effect" will be. Could it make tomcat crash? ? Will there be a "data consistency" problem? ?

The safest way is to increase the value of "wait_timeout" and set 28800 to a larger value, so there should be no problem. 
Add in my.ini under windows: 
interactive_timeout=28800000 
wait_timeout=28800000

Note: At present, I am using autoReconnect, and I have not found any problems.

 

Reprinted to: 
http://blog.csdn.net/liuxiaogangqq/article/details/41757501

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326449937&siteId=291194637