1

My server started to hang at least once a day. It remains so until I restart Tomcat. I'm not sure yet which part causes the problem, so I'm trying to work on several hypotheses:

  • My website is served through Google's PageSpeed, so I doubt I'm under a DDoS attack.
  • When it hangs, I have to restart both apache2 and tomcat. So I can't tell which one is causing the problem.
  • When the server hangs, I check mysql's processlist, and I find a dozen of idle connections. This means that the database is not overloaded.
  • When the server hangs, I see few errors like this one. So it it could be the case that the problem resides within the connection between my Java code and MySQL:

    java.sql.SQLException: Operation not allowed after ResultSet closed
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
        at com.mysql.jdbc.ResultSetImpl.checkClosed(ResultSetImpl.java:734)
        at com.mysql.jdbc.ResultSetImpl.checkRowPos(ResultSetImpl.java:778)
        at com.mysql.jdbc.ResultSetImpl.getObject(ResultSetImpl.java:4419)
        at com.mypackage.SqlUtils.getResultMapList(SqlUtils.java:66)
    

My centralized code for retrieving results from the database is:

public List<ResultMap> getResultMapList(String sql) {
    List<ResultMap> rows = new LinkedList<>();
    try (Connection conn = getConnection();
            ResultSet res = conn.createStatement().executeQuery(sql)) {
        while (res.next()) {
            ResultMap row = new ResultMap();
            ResultSetMetaData meta = res.getMetaData();
            for (int i = 1; i <= meta.getColumnCount(); i++) {
                row.put(meta.getColumnLabel(i).toLowerCase(), res.getObject(i)); // Line 66
            }
            rows.add(row);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rows;
}

My connection is pooled:

public Connection getConnection() {
    if (datasource == null) {
        PoolProperties p = new PoolProperties();
        p.setUrl(myURL);
        p.setDriverClassName("com.mysql.jdbc.Driver");
        p.setUsername(dbprefix + "user1");
        p.setPassword(password);
        p.setJmxEnabled(true);
        p.setTestWhileIdle(false);
        p.setTestOnBorrow(true);
        p.setValidationQuery("SELECT 1");
        p.setTestOnReturn(false);
        p.setValidationInterval(30000);
        p.setTimeBetweenEvictionRunsMillis(1000);
        p.setMaxActive(20);
        p.setMaxIdle(5);
        p.setInitialSize(5);
        p.setMaxWait(10000);
        p.setRemoveAbandonedTimeout(30);
        p.setMinEvictableIdleTimeMillis(10000);
        p.setMinIdle(2);
        p.setLogAbandoned(true);
        p.setRemoveAbandoned(true);
        p.setFairQueue(true);
        p.setJdbcInterceptors("org.apache.tomcat.jdbc.pool.interceptor.ConnectionState;"
                + "org.apache.tomcat.jdbc.pool.interceptor.StatementFinalizer");
        datasource = new DataSource();
        datasource.setPoolProperties(p);
    }
    Connection conn = null;
    try {
        conn = datasource.getConnection();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return conn;
}

ResultMap extends LinkedHashMap<String, Object>, and I'm using it only to store the fields retrieved in the ResultSet.

Software installed: Apache/2.4.7 (Ubuntu), Mysql: 5.5.40 (InnoDB), Tomcat: 8.0.15, Java: 1.8.0_25.

Any suggestions about why the server is hanging?

0

Maybe it is logrotate, which is restarting mysql and then your connection to the db needs to be closed + reopened?

  • Why would logrotate restart mysql? – mossaab Nov 27 '14 at 1:30
  • in order to close and reopen the logs. well actually this should not require a restart, but still maybe? – edlerd Nov 27 '14 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.