MySQL automatically disconnects all connections every eight hours when the database connection pool is connected to MySQL.
I hate this problem recently. In our project, we use our connection pool to connect to the MySQL database. However, every eight hours, the MySQL database will automatically disconnect all connections and the connection pool will become invalid, you need to restart tomcat to make it effective. Ha ha, the server cannot 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:
Add autoreconnect = true when connecting to the database:
JDBC: mysql: // localhost: 3306/EMS? Autoreconnect = true & useunicode = true & characterencoding = GBK
However, the MySQL manual contains the following:
Autoreconnect |
DriverProgramShould I try to establish a valid and/or dead connection again? If it is allowed, the driver throws an exception for queries sent over invalid or dead connections (belonging to the current transaction), but when the next query is sent over the connection of the new transaction, will try again.This feature is not recommended because it causes side effects related to session Status and data consistency when the application cannot properly process sqlexceptions.It is designed only for the following scenarios, that is, when you cannot configure an application to properly handle sqlexceptions caused by dead connections and/or invalid connections. As an optional method, you can set the MySQL server variable "wait_timeout" to a high value instead of the default 8 hours. |
Haha, I don't know what the "side effects" will cause. Will it cause Tomcat to crash ?? Will there be a "data consistency" problem ??
To be more secure, add the value of "wait_timeout" and set "28800" to a larger value, so that there is no problem.
Note: I am currently using autoreconnect and have not found any problems.