一个实用的建议是将一些调试代码和“记录”结果集的创建和关闭添加到 csv 文件中。稍后您可以检查此文件并检查每个“创建”是否有一个“关闭”条目。
因此,假设您有一个带有静态方法的实用程序类,允许将字符串写入文件,您可以这样做:
ResultSet rs = stmt.executeQuery(query);
Util.writeln(rs.hashcode() + ";create"); // add this line whenever a
// new ResultSet is created
和
rs.close();
Util.writeln(rs.hashcode() + ";closed"); // add this line whenever a
// ResultSet is closed
使用 Excel 或任何其他电子表格程序打开 csv 文件,对表格进行排序并查看结果集是否未关闭。如果是这种情况,请添加更多调试信息以清楚地识别开放集。
顺便说一句 - 包装接口(如 JAMon)非常容易,如果你有 eclipse 或其他东西,它的编码时间不到 15 分钟。您需要包装 Connection、Statement(和 PreparedStatement?)和 ResultSet,ResultSet 包装器可以用于跟踪和监控结果集的创建和关闭:
public MonitoredConnection implements Connection {
Connection wrappedConnection = null;
public MonitoredConnection(Connection wrappedConnection) {
this.wrappedConnection = wrappedConnection;
}
// ... implement interface methods and delegate to the wrappedConnection
@Override
public Statement createStatement() {
// we need MonitoredStatements because later we want MonitoredResultSets
return new MonitoredStatement(wrappedConnection.createStatemet());
}
// ...
}
MonitoredStatement 和 MonitoredResultSet 相同(MonitoredStatement 将返回包装好的结果集):
public MonitoredStatement implements Statement {
private Statement wrappedStatement = null;
@Override
public ResultSet executeQuery(String sql) throws SQLException
MonitoredResultSet rs = wrappedStatement.executeQuery(sql);
ResultSetMonitor.create(rs.getWrappedResultSet()); // some static utility class/method
return rs;
}
// ...
}
和
public MonitoredResultSet implements ResultSet {
private ResultSet wrappedResultSet;
@Override
public void close() {
wrappedResultSet.close();
ResultSetMonitor.close(wrappedResultSet); // some static utility class/method
}
// ...
}
最后,您应该只需要修改代码中的一行:
Connection con = DriverManager.getConnection(ur);
到
Connection con = new MonitoredConnection(DriverManager.getConnection(ur));