Java connectivity issue
I am getting following error.
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 37,787,544 milliseconds ago. 开发者_高级运维The last packet sent successfully to the server was 37,787,544 milliseconds ago. is longer than the server configured value of 'wait_timeout'. You should consider either expiring and/or testing connection validity before use in your application, increasing the server configured values for client timeouts, or using the Connector/J connection property 'autoReconnect=true' to avoid this problem.
Once I restart the application, it works OK for some time and then the same error comes up.
There are a couple of things you can do,
- configure wait_timeout to a very long value a day or two. See here
- Appenf autoReconnect=true in your MySQL connection URL
jdbc:mysql://yourhost:port/dbname?autoReconnect=true
Your connections are expiring because you're creating them and keeping them "forever".
The best approach is to use a connection pool, such as Apache's DBCP - DataBase Connection Pool.
One of the things a pool gives you is it checks the validity of the connection before giving it to you - if the connection is "no good" (it runs a simple query to assert this), it closes it and creates a new one then give you the new one. This neatly caters for expiration.
精彩评论