开发者

Java error when caching

开发者 https://www.devze.com 2023-03-31 02:12 出处:网络
I have a game and when I cache items with this method it works fine when I first get in-game public void add(IItem item, int priceId, int priceAmount) {

I have a game and when I cache items with this method it works fine when I first get in-game

public void add(IItem item, int priceId, int priceAmount) {
    try {
    MapleInventoryManipulator.removeFromSlot(c, MapleItemInformationProvider.getInstance().getInventoryType(item.getItemId()), item.getPosition(), item.getQuantity(), true);
    MarketItem m = new MarketItem(getPlayer().getName(), (IItem)item, priceId, priceAmount);
    MarketItem s = new MarketItem(getPlayer().getName(), (IItem)item);
    //Market.add(m);
    Market.add(s);
   } catch (Exception e) {
   e.printStackTrace();
   }
    System.out.println(Market.getItems().get(0).getItem() == null ? "Item is null" : "Item is not null.");
}

public static void add(MarketItem item) {
    lock.writeLock().lock();
    try {
    try {
        itemCache.add(item); //try save directly to db see if i get stuck still
        System.out.println("Added item " + item.getName());
    } finally {
        lock.writeLock().unlock();
    }
   } catch (Exception e) {
   e.printStackTrace();
   }
}

I can cache new items unlimited times but then when I save with this method

public static void save() {
    try {
        PreparedStatement ps = DatabaseConnection.getConnection().prepareStatement("DELETE FROM market_items");
        ps.execute();
        ps.close();
        //After purging the table, insert updated values...
        List<MarketItem> equips = new LinkedList<MarketItem>();
        List<MarketItem> notEquips = new LinkedList<MarketItem>();
        lock.readLock().lock();
        try {
            for (MarketItem item : itemCache) {
                item.setType((byte)0);
                if (item.getInventoryType() == 1) {
                    equips.add(item);
                } else {
                    notEquips.add(item);
                }
            }
        } finally {
            lock.readLock().lock();
        }
        sLock.readLock().lock();
        try {
            for (String keySet : storage.keySet()) {
                for (MarketItem item : storage.get(keySet)) {
                    item.setType((byte)1);
                    if (item.getInventoryType() == 1) {
                        equips.add(item);
                    } else {
                        notEquips.add(item);
                    }
                }
            }
        } finally {
            sLock.readLock().unlock();
        }
        ps = DatabaseConnection.getConnection().prepareStatement("INSERT INTO market_items (`itemid`, `ownername`, `priceItem`, `priceAmount`, `itemname`, `inventorytype`, `type`) VALUES (?, ?, ?, ?, ?, ?, ?)");
        for (MarketItem notEquip : notEquips) {
            ps.setInt(1, notEquip.getItem().getItemId());
            ps.setString(2, notEquip.getOwner());
            ps.setInt(3, notEquip.getPriceItem());
            ps.setInt(4, notEquip.getPriceAmount());
       开发者_运维问答     ps.setString(5, notEquip.getName());
            ps.setByte(6, notEquip.getInventoryType());
            ps.setByte(7, notEquip.getType());
            ps.execute();
        }
        ps.close();
        ps = DatabaseConnection.getConnection().prepareStatement("INSERT INTO market_items (`itemid`, `ownername`, `priceItem`, `priceAmount`, `itemname`, `inventorytype`, `type`, `acc`, `avoid`, `dex`, `flag`, `hands`, `hp`, `int`, `itemexp`, `jump`, `level`, `luk`, `matk`, `mdef`, `mp`, `owner`, `speed`, `str`, `vicious`, `watk`, `wdef`) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        for (MarketItem equip : equips) {
            Equip eq = (Equip)equip.getItem();
            ps.setInt(1, equip.getItem().getItemId());
            ps.setString(2, equip.getOwner());
            ps.setInt(3, equip.getPriceItem());
            ps.setInt(4, equip.getPriceAmount());
            ps.setString(5, equip.getName());
            ps.setByte(6, equip.getInventoryType());
            ps.setByte(7, equip.getType());
            ps.setShort(8, eq.getAcc());
            ps.setShort(9, eq.getAvoid());
            ps.setShort(10, eq.getDex());
            ps.setShort(11, eq.getFlag());
            ps.setShort(12, eq.getHands());
            ps.setShort(13, eq.getHp());
            ps.setShort(14, eq.getInt());
            ps.setInt(15, eq.getItemExp());
            ps.setShort(16, eq.getJump());
            ps.setShort(17, eq.getLevel());
            ps.setShort(18, eq.getLuk());
            ps.setShort(19, eq.getMatk());
            ps.setShort(20, eq.getMdef());
            ps.setShort(21, eq.getMp());
            ps.setString(22, eq.getOwner());
            ps.setShort(23, eq.getSpeed());
            ps.setShort(24, eq.getStr());
            ps.setShort(25, eq.getVicious());
            ps.setShort(26, eq.getWatk());
            ps.setShort(27, eq.getWdef());
            ps.execute();
        }
        ps.close();
    } catch (SQLException e) {
        System.out.println("Exception caught saving market items: ");
        e.printStackTrace();
    }
}

It works fine when saving into the database, but now when I'm trying to cache an item with the add command, my code gets stuck.

Should I be disposing the cache or doing something else?


More info is required to fully determine what's going wrong, but "my code gets stuck" sounds like a locking issue.

At first glance, the glaring error looks like this block in your save method:

finally {
   lock.readLock().lock();
}

That looks like it should be an unlock.

0

精彩评论

暂无评论...
验证码 换一张
取 消