[JAVA] Issuing SQL consecutively results in com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:

I've been addicted to MySQL for a long time, so make a note.

Symptoms

If you issue SQL twice in a row com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure An error occurs and an empty table is returned. Immediately after the first issue, redirect jumps to another page, and when the next SQL is issued there, an error occurs. However, immediately after that, if you manually refresh the page using the browser with the same URL, the correct table will be displayed.

Background

I was instructed as a learning task and created a CRUD HP. When I was using h2database of in-memory DB on the IDE embedded local server by STS (Eclipse + Spring kit), I was told to change the DB to MySQL and run it in server mode. Therefore, I built a CentOS + MySQL environment using Vagrant with reference to dot installation and reconnected it. Then, the DB that was working normally before the change stopped working in some places. One of them is the above symptom.

What is considered a problem

Even though the close () of the first DB connection was not completed, the number of connectables was exceeded when trying to make an additional connection, or the SQL processing could not catch up and there was a conflict. Currently, DB connection and SQL issuance are performed as a set, so it is not yet known at which point it has stopped.

Anyway solution

Before the first SQL issue + sshConnection close and the second SQL issue


		try {
			Thread.sleep(3000);//if here is not this,caused MySQL error.communication link failer.
		}catch(InterruptedException e) {e.printStackTrace();}

I put it in and it worked for the time being.

What I guessed from the result

There is a big difference in response speed between the in-memory embedded database and the virtual server. Since both are in the same PC, there should be no difference in response speed, but are you daring to simulate the time required for communication in order to produce problems that may occur on the production server?

Originally, it is not the above support, and it may not be a fundamental solution unless you change the MySQL environment settings such as increasing the number of connectables. If you know the correct solution, we welcome your comments.

Recommended Posts

Issuing SQL consecutively results in com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
Run raw sql in Hanami to get results not in columns