Laurent Hinoul

“Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live.” - John F. Woods

How to fix Can not read response from server for MySQL

Hi, I made a java program where much database queries are processed in MySQL. After some time running, I got an error message:

After some research, it seems that the MySQL driver is not reconnecting after it lost its connection to the database.

First, append the following line ?autoReconnect=true to your jdbc connection string:

Next, you must find out what the wait timeout is of your MySQL. Run the query ‘show variables’ a search for wait_timeout. For me, it has the value 60.

Now we know the timeout, we can adjust our data source. I use Hibernate and C3P0. First, make sure you have the following dependencies to make hibernate and C3P0 work correctly:

Now we edit the data source values. Just make sure that the size of ‘idleConnectionTestperiod’ is smaller than your wait_timeout:

It’s done! Nice work! I hope it did the trick for you too. Enjoy!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: