com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure 
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused
Caused by: java.net.UnknownHostException: localhost: unknown error
Caused by: java.net.ConnectException: Connection timed out

The spring boot exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure happens when the java mysql database driver is unable to connect to mysql database. The Java application was unable to establish communication between mysql database driver and mysql database. The Jdbc connection between the java and mysql database could not be established. Therefore, the java exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure will be thrown.

Java Database Connectivity (JDBC) is used to connect a database to a Java application. The database driver helps to create a connection between the java and the database, and performs query execution in the java application database. If the database driver fails to establish a connection or a database communication link failure, the database exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure will be thrown.



Exception

The com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure exception will be shown as below stack trace

2020-12-01 04:11:37.796 ERROR 12682 --- [           main] com.zaxxer.hikari.pool.HikariPool        : HikariPool-1 - Exception during pool initialization.

com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at com.mysql.cj.jdbc.exceptions.SQLError.createCommunicationsException(SQLError.java:174) ~[mysql-connector-java-8.0.21.jar:8.0.21]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226) ~[spring-boot-2.3.4.RELEASE.jar:2.3.4.RELEASE]
	at com.yawintutor.SpringBootTransactionApplication.main(SpringBootTransactionApplication.java:10) ~[classes/:na]
Caused by: com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_101]
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_101]
	at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956) ~[mysql-connector-java-8.0.21.jar:8.0.21]
	at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826) ~[mysql-connector-java-8.0.21.jar:8.0.21]
	... 49 common frames omitted
Caused by: java.net.ConnectException: Connection refused
	at java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:1.8.0_101]
	at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) ~[na:1.8.0_101]
	at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) ~[na:1.8.0_101]
	at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) ~[na:1.8.0_101]
	at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) ~[na:1.8.0_101]
	at java.net.Socket.connect(Socket.java:589) ~[na:1.8.0_101]
	at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:155) ~[mysql-connector-java-8.0.21.jar:8.0.21]
	at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65) ~[mysql-connector-java-8.0.21.jar:8.0.21]
	... 52 common frames omitted


Root Cause

The java database connectivity is responsible to establish the connection and execute the query in the database. The database driver creates a connection with the database and performs the query execution. If the database is unable to establish the connection or the communication link failure in the established connection, the exception com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure will be thrown.



Solution 1 – Mysql database is shutdown

The java application attempts to connect to the mysql server that is not running. The mysql database server may not be started or shutdown. Login to the mysql database server, verify the database is running or shutdown. Starting or restarting the mysql database will resolve the exception “com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure”.

start or restart mysql database


Solution 2 – MySql run out of connections

The mysql database creates and runs maximum configured connections. If a new connection is tried by the java program, the mysql database will run out of connections. The restart of the mysql server releases the connection and establishes new connections. If this problem happens regularly, the maximum connection number should be raised.

restart mysql database
increase maximum allowed concurrent connections in mysql database
verify any database connection leak in java


Solution 3 – IP address or hostname in JDBC URL is wrong

The host name or IP address that the Java programme configures might not be right. In the configuration, check the host name or the IP address. Try modifying the IP address and host name. In mysql jdbc url, the host name and the IP address may be misspelt.

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect


Solution 4 – Port number is wrong or missing

The mysql database is executed on a port. 3306 is the default port. You should configure the MySQL database to run on various ports. Check the port of the mysql database and configure the right port number in the jdbc url. The port number could be missing in the jdbc url.

spring.datasource.url=jdbc:mysql://localhost:3306/test


Solution 5 – DNS server issues

The DNS server will not recognise the host name configured on the jdbc url. The host name may not be configured or there may be any DNS mapping issues. Restarting a DNS server addresses an exception or uses an IP address in the JDBC url instead of the host name.

verify the host name using ping command - ping localhost
try to connect host name using telnet command - telnet localhost 3306
restart the dns server
use IP address instead of the host name


Solution 6 – Mysql server issue

The mysql database server does not accept the TCP/IP connection. The mysql database server may not respond due to memory issues or hang the server due to long running hours / days. Restarting the mysql database server will release the port, memory issues such as OutOfMemoryError, Heap Memory error, cpu utilization

restart mysql database server
verify the host name using ping command - ping localhost
try to connect host name using telnet command - telnet localhost 3306


Solution 7 – Network issues

There could be some firewalls, proxy, antivirus softwares blocks the connection. Verify and disable the firewall, proxy settings. If an antivirus software blocks the connection, enable the antivirus to allow the connection. If the network speed is slow, there could be a timed out error. Make sure the network such as internet speed is good to establish the connection.

disable firewall, proxy
allow the connection in antivirus
verify the network, internet speed to establish connection


Solution 8 – Maven dependency

In the java application, use the correct maven dependency for the mysql database version. The mismatch of the mysql database version and the mysql database driver jar version will create the connectivity issue. make sue the right maven dependency is configured for the mysql database driver corresponding to the mysql database version.

		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *