繁体   English   中英

eclipe:间歇性com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败

[英]eclipe: Intermittent com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure

对此有很多问题,但是似乎没有任何问题可以解决我的问题。 我收到“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败”错误,但仅在成功处理约32000条记录后才出现。 我已成功连接到MySQL。 我已成功读写,因此它不是本地主机/端口/绑定地址/等问题。

我有一个应用程序,它读取CSV文件并处理每一行数据,读取一些数据库表,插入其他表并更新其他表。 我仔细检查了我的代码,并关闭了与数据库的每个连接。 我可以通过MySQL Workbench观察MySQL服务器的状态,负载相当稳定在0.5,连接范围是2-15,并且似乎关闭得很好,其他一切看起来都非常稳定并且在正常范围内。

但是在32000和34000记录之间转换为75000记录CSV文件,我得到了上面的错误。 它并不总是在同一条记录上结束,并且它结束的记录似乎也没有什么不足。

我有什么想法可以诊断出来吗?

这是我用于所有MySQL连接的代码:

public class MySQLAccess {
    private Connection connect = null;
    private Statement statement = null;
    private ResultSet resultSet = null;
    public String query = null;
    public int lastId = 0;

    private Connection getConnection() throws Exception {
        // reads a config properties file
        AWBConfig Config = new AWBConfig();
        String host = Config.Data.getProperty("mysql.host");
        if (!Config.Data.getProperty("mysql.port").isEmpty()) host += ":" + Config.Data.getProperty("mysql.port");
        Class.forName("com.mysql.jdbc.Driver");
        return DriverManager.getConnection("jdbc:mysql://"+host+"/" + Config.Data.getProperty("mysql.name") + "?user=" + 
            Config.Data.getProperty("mysql.user") + "&password=" + Config.Data.getProperty("mysql.pass"));
    }

    public ResultSet readDataBase() throws Exception {
        if (connect == null || connect.isClosed()) connect = getConnection();
        statement = connect.createStatement();
        resultSet = statement.executeQuery(query);
        return resultSet;
    }

    public ResultSet readDataBase(String _query) throws Exception {
        this.query = _query;
        resultSet = this.readDataBase();
        return resultSet;
    }

    public void writeData() throws Exception {
        if (connect == null || connect.isClosed()) connect = getConnection();
        statement = connect.createStatement();
        if (query.substring(0, 6).equals("INSERT")) {
            statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
            ResultSet rs = statement.getGeneratedKeys();
            if (rs.next()){
                this.lastId=rs.getInt(1);
            }
            rs.close();
        } else {
            statement.executeUpdate(query);
            this.lastId = -1;
        }
    }

    public void close() {
        try {
            if (resultSet != null) resultSet.close();
            if (statement != null) statement.close();
            if (connect != null) connect.close();
        } catch (Exception e) {
        }
    }
}

然后与数据库交互,我使用这种格式:

    protected void getCampaignById() throws Exception {
        _dba = new MySQLAccess();
        _dba.query = "SELECT * FROM table WHERE id = '" + String.valueOf(id) + "'";
        ResultSet rs = _dba.readDataBase();
        if (rs.next()) {
            ... do stuff with rs
        }
        _dba.close();
    }

好吧,我不确定到底是什么问题,但是我在核心应用程序的main()方法中将_dba更改为静态变量,然后将以下行添加到每个类构造函数中:

_dba = MyMainClass._dba;

然后从所有地方删除这些行(MyMainClass.main()除外):

_dba = new MySQLAccess();
...
_dba.close();

使数据库连接成为全局持久连接,不仅解决了我的错误,而且还极大地提高了性能。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM