所以我正在开展一个项目,我不确定如何处理这个错误或者可以做些什么来解决它。我已经尝试了我能想到的一切,也许其他人对如何修复它有一些意见。它用java编码,适用于游戏。错误是资源泄漏:' rset'在这个位置没有关闭。这就是它挑出的那条线。
rset = statement.executeQuery("SELECT * FROM `items` WHERE `owner_id` = " + objectId + " AND `location` = " + ItemLocation.EQUIPMENT.ordinal());
希望有人对如何修复此错误有一些意见,如果我禁止警告,是否会导致内存泄漏或影响程序的性能?
public final PlayerPreview restorePreview(int objectId) {
Connection con = null;
PreparedStatement statement = null;
ResultSet rset = null;
try {
con = connectFactory.getConnection();
statement = con.prepareStatement(SELECT_PLAYER_PREVIEW);
statement.setInt(1, objectId);
rset = statement.executeQuery();
if(!rset.next()) {
LOGGER.warning("not found player for " + objectId);
return null;
}
PlayerPreview playerPreview = PlayerPreview.newInstance(objectId);
playerPreview.setSex(rset.getByte("sex"));
playerPreview.setRaceId(rset.getByte("race_id"));
playerPreview.setClassId(rset.getByte("class_id"));
playerPreview.setLevel(rset.getByte("level"));
playerPreview.setOnlineTime(rset.getLong("online_time"));
playerPreview.setName(rset.getString("char_name"));
PlayerAppearance appearance = loadPlayerAppearance(objectId);
if(appearance == null)
return null;
Equipment equipment = PlayerEquipment.newInstance(null);
ItemTable itemTable = ItemTable.getInstance();
{
rset = statement.executeQuery("SELECT * FROM `items` WHERE `owner_id` = " + objectId + " AND `location` = " + ItemLocation.EQUIPMENT.ordinal());
while(rset.next()) {
ItemTemplate template = itemTable.getItem(rset.getInt("item_id"));
if(template == null)
continue;
ItemInstance item = template.newInstance(rset.getInt("object_id"));
item.setIndex(rset.getInt("index"));
item.setLocation(ItemLocation.VALUES[rset.getInt("location")]);
item.setOwnerId(objectId);
item.setItemCount(rset.getLong("item_count"));
item.setEnchantLevel(rset.getShort("enchant_level"));
item.setBonusId(rset.getInt("bonus_id"));
item.setAutor(rset.getString("autor"));
equipment.setItem(item, item.getIndex());
}
}
playerPreview.setAppearance(appearance).setEquipment(equipment);
return playerPreview;
} catch(SQLException e) {
LOGGER.warning(e);
} finally {
DBUtils.closeDatabaseCSR(con, statement, rset);
}
return null;
}
答案 0 :(得分:1)
在您检查"找不到播放器"后,您应该关闭rset
。
您之后正在执行新查询,但未关闭之前的结果集 - >资源泄漏。