Resource Leak: 'rset' is not closed at this location

11.4k views Asked by At

So i am working on a project and I'm unsure how to deal with this error or what can be done to fix it. I've tried everything i could think of, maybe someone else has some input on how to fix it. it is coded in java and is for a game. The error is Resource leak: 'rset' is not closed at this location. it is this line it singles out.

rset = statement.executeQuery("SELECT * FROM `items` WHERE `owner_id` = " + objectId + " AND `location` = " + ItemLocation.EQUIPMENT.ordinal());

Hopefully someone has some input on how to fix this error, should i suppress the warning, will it cause a memory leak or affect the performance of the program?

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;
}
1

There are 1 answers

0
Kayaman On BEST ANSWER

You should close the rset after you're done checking for "not found player".

You're executing a new query afterwards, yet not closing the previous resultset -> resource leak.