简体   繁体   中英

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

There are a bunch of questions on this, but nothing that seems to address my issue. I am getting the "com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure" error but only after processing about 32000 records successfully. I am successfully connecting to MySQL. I am successfully reading and writing, so its not a localhost / port / bind-address / etc issue.

I have an app that reads a CSV file and processes each line of data, reading some db tables, inserting to others and updating other tables. I've double checked my code and I close each connection to the database. I can watch the MySQL server status through MySQL Workbench, Load is fairly steady at 0.5, connections range from 2 - 15 and seem to be closing fine, everything else seems pretty steady and withing normal range.

But between 32000 and 34000 records into a 75000 record CSV file, I get the above error. It doesn't always end on the same record, and there doesn't seem to be anything off about the record that it does end on.

Any ideas where I could look to diagnose this?

Here is my code that I am using for all MySQL connections:

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) {
        }
    }
}

and then to interact with the database, I use this format:

    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();
    }

Well, I'm not sure what the issues entirely were, but I changed _dba to be a static variable in my core app's main() method, and then added the following line to each class constructor:

_dba = MyMainClass._dba;

then removed these lines from everywhere (except MyMainClass.main()):

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

Making the database connection a global persistent connection and not only did it solve my error, but it also gave me a huge performance boost.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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