JDBC Error: ResultSet object has been closed
When I am executing a ResultSet.next(), I am getting the error ResultSet object has been closed. This is even happening within the loop processing the ResultSet. The loop executes the .next() but the first call to get a result returns the error. I am getting pretty small datasets, typically less than 10 records, just a pair of longs.
The maddening part of this is that with identical inputs, I only see the error about 10% of the time. Seems like a problem between the database and the app server, but I am not 100% sure.
I'm hoping someone can point out where I have gone wrong or what I could do to mitigate the problem.
public Map<Long, Long> getUsersForBeacons(List<Long> beaconIds) throws DbException {
StringBuilder sql = new StringBuilder();
sql.append("select beacon_id, participant_id from participant where beacon_id in (");
int count = beaconIds.size();
for (int i = 0; i < count - 1; i++) {
sql.append("?,");
}
sql.append("?)");
checkConnection();
try {
PreparedStatement statement = mConn.prepareStatement(sql.toString());
for (int i = 0; i < count; i++) {
statement.setLong(i + 1, beaconIds.get(i));
}
Map<Long, Long> result = new HashMap<>();
long tempBeaconId;
long tempParticipantId;
ResultSet rows = statement.executeQuery();
while (rows.next()) {
if(!rows.isClosed()) {
tempBeaconId = rows.getLong("beacon_id");
tempParticipantId = rows.getLong("participant_id");
if(tempBeaconId > 0 && tempParticipantId > 0) {
result.put(tempBeaconId, tempParticipantId);
}
} else {
//This definitely happens
LOG.warn("ResultSet closed during loop");
}
}
rows.close();
if (!statement.isClosed()) {
statement.close();
}
return result;
} catch (SQLException e) {
LOG.error(e.getStackTrace());
LOG.error("getUsersForBeacons database error", e);
throw new DbException(e);
}
}
private void checkConnection() throws DbException {
try {
if (mConn == null || mConn.isClosed()) {
mConn = mDataSource.getConnection();
}
} catch (SQLException ex) {
throw new DbException(ex);
}
}
0
Comments
Hi,
Could you tell me the versions of the following that you're seeing this problem with?
Thanks!