首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >eclipe:间歇性com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障

问eclipe:间歇性com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链路故障
EN

Stack Overflow用户
提问于 2015-07-07 05:04:42
回答 1查看 295关注 0票数 0

在这方面有一堆问题,但似乎没有解决我的问题。我收到"com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:通信链接失败“错误,但仅在成功处理了大约32000条记录之后。我正在成功连接到MySQL。我成功地读写了,所以这不是localhost / port / bind-address /等的问题。

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

但是在32000和34000条记录之间转换成一个75000条记录的CSV文件,我得到了上面的错误。它并不总是以相同的记录结束,并且似乎没有任何关于它结束的记录的任何东西。

有什么办法可以帮我诊断这个问题吗?

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

代码语言:javascript
复制
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) {
        }
    }
}

然后,为了与数据库交互,我使用以下格式:

代码语言:javascript
复制
    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();
    }
EN

回答 1

Stack Overflow用户

发布于 2015-07-08 01:00:32

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

代码语言:javascript
复制
_dba = MyMainClass._dba;

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

代码语言:javascript
复制
_dba = new MySQLAccess();
...
_dba.close();

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31255602

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档