2011-08-28 72 views
-1

時,我有一個遊戲,當我緩存使用這種方法的項目,當我第一次得到在遊戲中Java錯誤緩存

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(); 
    } 
} 

我可以緩存新項目,不限次數,但後來當我保存這個它工作正常方法

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(); 
    } 
} 

保存到數據庫的時候,但現在當我試圖緩存與add命令一個項目它工作正常,我的代碼卡。

我應該配置緩存還是做其他事情?

+0

顯示一些調試信息,發生錯誤的行,基本上更多的信息 –

+0

沒有錯誤,所以我沒有更多的信息 –

+0

如果沒有任何錯誤,你能描述什麼是行不通的。你還沒有問過問題。 – Preston

回答

2

需要更多信息才能完全確定發生了什麼問題,但「我的代碼卡住了」聽起來像是一個鎖定問題。

乍一看,該明顯的錯誤是這樣的塊在您保存方法:

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

這看起來像它應該是一個unlock