1

There seems to be enough of questions relating to CommunicationsException were posted on Stackoverflow. But none of them helps me out of a difficult position.

I installed MariaDB 5.5 in my CentOS 7 server, and configured everything so that I can connect to the DB server remotely through a MySQL Workbench or Data Source Explorer in eclipse. A screen capture of the succeed connections is like below. enter image description here

However, when I try to connect the DB Server via JDBC, it continues to fail with the Exception "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure".

The test class is shown as following.

package com.pratice;
import java.sql.*;
public class DatabaseConn {
    public static void main(String[] args) throws Exception{
        Connection conn = null;
        ResultSet rs = null;
        Statement stmt = null;
        String url = "jdbc:mysql://59.12.140.121:6330/myPrjs";//?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true
        String username = "user";
        String password = "password";
        String driver = "com.mysql.jdbc.Driver";

        try{
            Class.forName(driver);

            conn = DriverManager.getConnection(url, username, password);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("SELECT 1");

            while(rs.next()){
                System.out.print(rs.getObject(1) + "\t" + rs.getObject(2));
            }
        }finally{
            if(rs!=null) {  
                rs.close();  
            }  
            if(stmt!=null) {  
                stmt.close();  
            }  
            if (conn != null) {  
                conn.close();  
            }  
        }
    }
}

When I run DatabaseConn, I got the error messages.

Exception in thread "main" 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.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1127)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:356)
    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2502)
    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2539)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2321)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:832)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:409)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:417)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:344)
    at java.sql.DriverManager.getConnection(DriverManager.java:571)
    at java.sql.DriverManager.getConnection(DriverManager.java:215)
    at com.webjsp.pratice.DatabaseConn.main(DatabaseConn.java:18)
Caused by: java.net.SocketException: Bad address: connect
    at java.net.DualStackPlainSocketImpl.connect0(Native Method)
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339)
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200)
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182)
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
    at java.net.Socket.connect(Socket.java:579)
    at java.net.Socket.connect(Socket.java:528)
    at java.net.Socket.<init>(Socket.java:425)
    at java.net.Socket.<init>(Socket.java:241)
    at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:258)
    at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:306)
    ... 15 more

I cannot solve this problem so I searched on the internet, and I found a checklist at here . But now that I can connect to my DB server via IDEs, I don't know what could be wrong.

I also tried to use org.mariadb.jdbc.driver instead of mysql driver, I got error messages too.

Exception in thread "main" java.sql.SQLNonTransientConnectionException: Could not connect to 59.12.140.121:6330 : Bad address: connect
        at org.mariadb.jdbc.internal.SQLExceptionMapper.get(SQLExceptionMapper.java:136)
        at org.mariadb.jdbc.internal.SQLExceptionMapper.throwException(SQLExceptionMapper.java:106)
        at org.mariadb.jdbc.Driver.connect(Driver.java:115)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at java.sql.DriverManager.getConnection(Unknown Source)
        at DatabaseConn.main(DatabaseConn.java:16)
Caused by: org.mariadb.jdbc.internal.common.QueryException: Could not connect to 59.12.140.121:6330
: Bad address: connect
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:714)
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.<init>(MySQLProtocol.java:280)
        at org.mariadb.jdbc.Driver.connect(Driver.java:111)
        ... 3 more
Caused by: java.net.SocketException: Bad address: connect
        at java.net.DualStackPlainSocketImpl.connect0(Native Method)
        at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source)
        at java.net.AbstractPlainSocketImpl.connect(Unknown Source)
        at java.net.PlainSocketImpl.connect(Unknown Source)
        at java.net.SocksSocketImpl.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at java.net.Socket.connect(Unknown Source)
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:387)
        at org.mariadb.jdbc.internal.mysql.MySQLProtocol.connect(MySQLProtocol.java:709)
        ... 5 more

Could anyone tell me how can I solve this problem, or give me some hints that why does it happening? Thank you so much.

Community
  • 1
  • 1
sincosmos
  • 370
  • 2
  • 7
  • 15
  • @gustafbstrom I guess not. I configured the firewall settings, and successfully connect to my DB server remotely via MySQL Workbench. – sincosmos Apr 24 '15 at 13:08

1 Answers1

0

The key point to figure the problem out is at "java.net.SocketException: Bad address: connect".

The problem was caused by firewall rules at the client side (My Windows PC). I enabled Ahnlab V3 Internet Security Personal Firewall on my client PC. The outbound rules only allow certain types of outgoing socket by default. When I tried to connect to my remote server with a workbench, there was no problem. But when I tried to connect to the server by running the java Class, the local firewall kept the socket, so the connection requests did not even go through my network card! I add a new outbound rule which allows mysql outbound connection, and then my program runs normally.

Hope this will help anyone who encounters the same problem.

sincosmos
  • 370
  • 2
  • 7
  • 15