“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障"到远程数据库

quot;com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failurequot; to remote database(“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障到远程数据库)

本文介绍了“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障"到远程数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试连接到远程 MySQL 数据库,但失败并出现此错误.

I tried to connect to my remote MySQL database, but I failed and got this error.

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

困惑的是,我使用MySQL-Front工具连接远程数据库时有效,并且可以成功ping到IP地址.但是当我使用我的代码时,它会在大约十秒钟后显示错误.

The confusion is that It was effective when I used MySQL-Front tool to connect the remote database, and I can ping to the IP address successfully. but when I used my code, it wound show the error after about ten seconds.

此外,当我在代码中使用错误的用户名或密码时,它会立即显示错误的验证.这是否证明建立连接没有问题?

Also when I used the wrong username or password in my code, it wound show the wrong verification immediately. Did that prove it is no problem to set up conection?

这是我的代码(它可以在我的本地主机数据库上运行):

Here is my code(It can work on my localhost database):

public static void main(String[] args) {
        String url = "jdbc:mysql://(IP address):3306/";
        String dbName = "talk";
        String driver = "com.mysql.jdbc.Driver";
        String userName = "talkroot";
        String password = "123456";
        try {
            Class.forName(driver).newInstance();
            Connection conn = DriverManager.getConnection(url + dbName,
                    userName, password);
            System.out.println("connect");
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("end");
    }

这是错误:

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

Last packet sent to the server was 0 ms ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2120)
    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:723)
    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:46)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:302)
    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:282)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at java.sql.DriverManager.getConnection(Unknown Source)
    at com.test.TestMySQL.main(TestMySQL.java:16)
Caused by: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

Last packet sent to the server was 21298 ms ago.
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:406)
    at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1074)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:3009)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2895)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3438)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1951)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2101)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2548)
    at com.mysql.jdbc.ConnectionImpl.configureClientCharacterSet(ConnectionImpl.java:1802)
    at com.mysql.jdbc.ConnectionImpl.initializePropsFromServer(ConnectionImpl.java:3444)
    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2062)
    ... 12 more
Caused by: java.net.SocketException: Software caused connection abort: recv failed
    at java.net.SocketInputStream.socketRead0(Native Method)
    at java.net.SocketInputStream.read(Unknown Source)
    at java.net.SocketInputStream.read(Unknown Source)
    at com.mysql.jdbc.util.ReadAheadInputStream.fill(ReadAheadInputStream.java:113)
    at com.mysql.jdbc.util.ReadAheadInputStream.readFromUnderlyingStreamIfNecessary(ReadAheadInputStream.java:160)
    at com.mysql.jdbc.util.ReadAheadInputStream.read(ReadAheadInputStream.java:188)
    at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:2452)
    at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2906)
    ... 20 more

谁能帮帮我?

推荐答案

如果你的数据库连接长时间空闲,就会出现com.mysql.jdbc.exceptions.jdbc4.CommunicationsException异常.此空闲连接在 connection.isClosed() 上返回 true;但是如果我们尝试执行语句,那么它会触发这个异常,所以我会建议使用数据库池.

This com.mysql.jdbc.exceptions.jdbc4.CommunicationsException exception occurs if your database connection is idle for long time. This idle connection returns true on connection.isClosed(); but if we try to execute statement then it will fire this exception so I will suggest to go with database pooling.

这篇关于“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障"到远程数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:“com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障"到远程数据库

基础教程推荐