Search

Suggested keywords:
  • Java
  • Docker
  • Git
  • React
  • NextJs
  • Spring boot
  • Laravel

How to solve CommunicationsException in Java while using Hibernate and MySQL

  • Share this:
post-title
You might have faced CommunicationsException, basically timeout issue in Java while connecting to MySQL using Hibernate. The session would be timed out after certain period of time. You might be thinking that the your site or application is running without any issue but it would have stopped or crashed due to below exception.

com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: The last packet successfully received from the server was 437,036,648 milliseconds ago. The last packet sent successfully to the server was 437,036,652 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.

To solve this issue, create a file c3p0.properties and add the below properties. Place the file in the same path of Hibernate.cfg.xml. This file should be part of the jar. c3p0.idleConnectionTestPeriod=100
c3p0.preferredTestQuery=SELECT 1
c3p0.testConnectionOnCheckin=true
c3p0.testConnectionOnCheckout=false
c3p0.debugUnreturnedConnectionStackTraces=true
com.mchange.v2.log.MLog=com.mchange.v2.log.log4j.Log4jMLog

Please post your feedback.

Editorial Team

About author
This article is published by our editorial team.