我正在尝试使用Java从我的MySQL数据库中导入一些数据,查询返回大约15,000行,并且为了防止ResultSet耗尽内存,我逐个传输每个结果。
private static void addEdges(DirectedGraph<String, DefaultEdge> g) throws SQLException {
Statement selectID = null;
String selectSQL = "SELECT citations.paperid, papers.id FROM papers INNER JOIN citations ON citations.title = papers.title AND citations.year = papers.year;";
try {
selectID = conn.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_READ_ONLY);
selectID.setFetchSize(Integer.MIN_VALUE);
ResultSet rs = selectID.executeQuery(selectSQL);
String from;
String to;
System.out.println("iterating");
while(rs.next()) {
from = rs.getString("paperid");
to = rs.getString("id");
g.addEdge(from, to);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
if (selectID != null) {
try {
selectID.close();
} catch (Exception e) {
System.out.println("Error closing statement");
}
}
}
}
但是我现在收到此错误:
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.mysql.jdbc.MysqlIO.nextRow(MysqlIO.java:2092)
at com.mysql.jdbc.RowDataDynamic.nextRecord(RowDataDynamic.java:406)
at com.mysql.jdbc.RowDataDynamic.next(RowDataDynamic.java:385)
at com.mysql.jdbc.RowDataDynamic.close(RowDataDynamic.java:163)
at com.mysql.jdbc.ResultSetImpl.realClose(ResultSetImpl.java:7472)
at com.mysql.jdbc.ResultSetImpl.close(ResultSetImpl.java:919)
at com.mysql.jdbc.StatementImpl.realClose(StatementImpl.java:2617)
at com.mysql.jdbc.StatementImpl.close(StatementImpl.java:580)
这是从selectID.close()
引发的。这是奇怪的,因为它应该关闭的ResultSet应该只包含一行。尝试分配更多内存,但这个问题仍然存在,任何想法?
(我正在使用JGraphT库来创建有向图)