简体   繁体   中英

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,

  1. configure wait_timeout to a very long value a day or two. See here
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM