ADSquared
ADSquared

Reputation: 237

MAC OS com.mysql.cj.exceptions.CJCommunicationsException: Communications link failure

I am trying to figure out why my app has trouble connecting to my database, only on mac OS so I'm using a small program to run on terminal on OS to see what was causing that.

It seems the hostname is causing the issue, but my service provider cannot give me an ip address to try if it fixes my issue. Does anyone have a fix for this or an idea on what I could try? Thanks a lot for your time.

here is the connection program. the database user can select only, the hostname is correct and password as well. This issue only happens on MAC OS and not all the time. My database accepts conneciton from all ip, I used a wildcard. Any idea on what to try next is appreciated.

import java.sql.Connection;
import java.sql.DriverManager;



public class test
{
 public static void main(String[] args) {
 System.out.println("\nMySQL JDBC Connection To G4THER DBase Tester");
 Connection conn = null;


 try {
  Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
  String userName = "app";
  String password = "password";
  String url = "jdbc:mysql://hostname:3306/dbase";
  conn = DriverManager.getConnection(url, userName, password);
  System.out.println("\nDatabase Connection Established...");
  Thread.sleep(1500L);
 } 
 catch (Exception ex) {

  System.err.println("Cannot connect to database server");
  ex.printStackTrace();

 }
 finally {

  if (conn != null)

    try {

      System.out.println("\nNow Lets terminate the Connection...");
      Thread.sleep(1500L);
      conn.close();
      System.out.println("\nDatabase connection terminated...");
      Thread.sleep(1500L);
      System.out.println("\n***** Everything works fine Ol' man *****");
    }
    catch (Exception ex) {

      System.out.println("Error in connection termination!");
    }  
  } 
 }
}

here is the error log.

Last login: Thu Dec 12 12:25:13 on ttys000

Michaels-iMac:~ michael$ cd downloads

Michaels-iMac:downloads michael$ java -jar dbasetester.jar

***** MySQL JDBC Connection To G4THER DBase Tester *****

Cannot connect to database server

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)

at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:64)

at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:836)

at com.mysql.cj.jdbc.ConnectionImpl.(ConnectionImpl.java:456)

at com.mysql.cj.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:246)

at com.mysql.cj.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:199)

at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:677)

at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)

at test.main(test.java:18)

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 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)

at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)

at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)

at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)

at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:61)

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:105)

at com.mysql.cj.exceptions.ExceptionFactory.createException(ExceptionFactory.java:151)

at com.mysql.cj.exceptions.ExceptionFactory.createCommunicationsException(ExceptionFactory.java:167)

at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:91)

at com.mysql.cj.NativeSession.connect(NativeSession.java:144)

at com.mysql.cj.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:956)

at com.mysql.cj.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:826)

... 6 more

Caused by: java.net.UnknownHostException: cpl81.hosting24.com: nodename nor servname provided, or not known

at java.base/java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method)

at java.base/java.net.InetAddress$PlatformNameService.lookupAllHostAddr(InetAddress.java:930)

at java.base/java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1499)

at java.base/java.net.InetAddress$NameServiceAddresses.get(InetAddress.java:849)

at java.base/java.net.InetAddress.getAllByName0(InetAddress.java:1489)

at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1348)

at java.base/java.net.InetAddress.getAllByName(InetAddress.java:1282)

at com.mysql.cj.protocol.StandardSocketFactory.connect(StandardSocketFactory.java:132)

at com.mysql.cj.protocol.a.NativeSocketConnection.connect(NativeSocketConnection.java:65)

... 9 more

Michaels-iMac:downloads michael$

thanks

Upvotes: 1

Views: 3056

Answers (1)

ADSquared
ADSquared

Reputation: 237

I was able to fix the issue using an IP address instead of the hostname. I used a site to find the IP with the hostname and changed my code. So far so good.

Upvotes: 1

Related Questions