refactor: use try-with-resources for item saving & loading db operations

This commit is contained in:
P0nk
2021-04-04 22:28:52 +02:00
parent fc34d54cdd
commit de14c5782d

View File

@@ -20,19 +20,16 @@
*/ */
package client.inventory; package client.inventory;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
import net.server.audit.locks.MonitoredLockType; import net.server.audit.locks.MonitoredLockType;
import net.server.audit.locks.factory.MonitoredReentrantLockFactory; import net.server.audit.locks.factory.MonitoredReentrantLockFactory;
import tools.DatabaseConnection; import tools.DatabaseConnection;
import tools.Pair; import tools.Pair;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Lock;
/** /**
* *
* @author Flav * @author Flav
@@ -150,10 +147,7 @@ public enum ItemFactory {
private List<Pair<Item, MapleInventoryType>> loadItemsCommon(int id, boolean login) throws SQLException { private List<Pair<Item, MapleInventoryType>> loadItemsCommon(int id, boolean login) throws SQLException {
List<Pair<Item, MapleInventoryType>> items = new ArrayList<>(); List<Pair<Item, MapleInventoryType>> items = new ArrayList<>();
PreparedStatement ps = null; try (Connection con = DatabaseConnection.getConnection()) {
ResultSet rs = null;
Connection con = DatabaseConnection.getConnection();
try {
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
query.append("SELECT * FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `"); query.append("SELECT * FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `");
query.append(account ? "accountid" : "characterid").append("` = ?"); query.append(account ? "accountid" : "characterid").append("` = ?");
@@ -162,138 +156,109 @@ public enum ItemFactory {
query.append(" AND `inventorytype` = ").append(MapleInventoryType.EQUIPPED.getType()); query.append(" AND `inventorytype` = ").append(MapleInventoryType.EQUIPPED.getType());
} }
ps = con.prepareStatement(query.toString()); try (PreparedStatement ps = con.prepareStatement(query.toString())) {
ps.setInt(1, value); ps.setInt(1, value);
ps.setInt(2, id); ps.setInt(2, id);
rs = ps.executeQuery();
while (rs.next()) { try (ResultSet rs = ps.executeQuery()) {
MapleInventoryType mit = MapleInventoryType.getByType(rs.getByte("inventorytype")); while (rs.next()) {
MapleInventoryType mit = MapleInventoryType.getByType(rs.getByte("inventorytype"));
if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) { if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) {
items.add(new Pair<Item, MapleInventoryType>(loadEquipFromResultSet(rs), mit)); items.add(new Pair<>(loadEquipFromResultSet(rs), mit));
} else { } else {
int petid = rs.getInt("petid"); int petid = rs.getInt("petid");
if (rs.wasNull()) { if (rs.wasNull()) {
petid = -1; petid = -1;
}
Item item = new Item(rs.getInt("itemid"), (byte) rs.getInt("position"), (short) rs.getInt("quantity"), petid);
item.setOwner(rs.getString("owner"));
item.setExpiration(rs.getLong("expiration"));
item.setGiftFrom(rs.getString("giftFrom"));
item.setFlag((short) rs.getInt("flag"));
items.add(new Pair<>(item, mit));
}
} }
Item item = new Item(rs.getInt("itemid"), (byte) rs.getInt("position"), (short) rs.getInt("quantity"), petid);
item.setOwner(rs.getString("owner"));
item.setExpiration(rs.getLong("expiration"));
item.setGiftFrom(rs.getString("giftFrom"));
item.setFlag((short) rs.getInt("flag"));
items.add(new Pair<>(item, mit));
} }
} }
rs.close();
ps.close();
con.close();
} finally {
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
}
} }
return items; return items;
} }
private void saveItemsCommon(List<Pair<Item, MapleInventoryType>> items, int id, Connection con) throws SQLException { private void saveItemsCommon(List<Pair<Item, MapleInventoryType>> items, int id, Connection con) throws SQLException {
PreparedStatement ps = null;
PreparedStatement pse = null;
ResultSet rs = null;
Lock lock = locks[id % lockCount]; Lock lock = locks[id % lockCount];
lock.lock(); lock.lock();
try { try {
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
query.append("DELETE `inventoryitems`, `inventoryequipment` FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `"); query.append("DELETE `inventoryitems`, `inventoryequipment` FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `");
query.append(account ? "accountid" : "characterid").append("` = ?"); query.append(account ? "accountid" : "characterid").append("` = ?");
ps = con.prepareStatement(query.toString());
ps.setInt(1, value);
ps.setInt(2, id);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("INSERT INTO `inventoryitems` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
if (!items.isEmpty()) { try (PreparedStatement ps = con.prepareStatement(query.toString())) {
for (Pair<Item, MapleInventoryType> pair : items) { ps.setInt(1, value);
Item item = pair.getLeft(); ps.setInt(2, id);
MapleInventoryType mit = pair.getRight(); ps.executeUpdate();
ps.setInt(1, value); }
ps.setString(2, account ? null : String.valueOf(id));
ps.setString(3, account ? String.valueOf(id) : null);
ps.setInt(4, item.getItemId());
ps.setInt(5, mit.getType());
ps.setInt(6, item.getPosition());
ps.setInt(7, item.getQuantity());
ps.setString(8, item.getOwner());
ps.setInt(9, item.getPetId()); // thanks Daddy Egg for alerting a case of unique petid constraint breach getting raised
ps.setInt(10, item.getFlag());
ps.setLong(11, item.getExpiration());
ps.setString(12, item.getGiftFrom());
ps.executeUpdate();
pse = con.prepareStatement("INSERT INTO `inventoryequipment` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); try (PreparedStatement psItem = con.prepareStatement("INSERT INTO `inventoryitems` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
if (!items.isEmpty()) {
for (Pair<Item, MapleInventoryType> pair : items) {
Item item = pair.getLeft();
MapleInventoryType mit = pair.getRight();
psItem.setInt(1, value);
psItem.setString(2, account ? null : String.valueOf(id));
psItem.setString(3, account ? String.valueOf(id) : null);
psItem.setInt(4, item.getItemId());
psItem.setInt(5, mit.getType());
psItem.setInt(6, item.getPosition());
psItem.setInt(7, item.getQuantity());
psItem.setString(8, item.getOwner());
psItem.setInt(9, item.getPetId()); // thanks Daddy Egg for alerting a case of unique petid constraint breach getting raised
psItem.setInt(10, item.getFlag());
psItem.setLong(11, item.getExpiration());
psItem.setString(12, item.getGiftFrom());
psItem.executeUpdate();
if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) { if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) {
rs = ps.getGeneratedKeys(); try (PreparedStatement psEquip = con.prepareStatement("INSERT INTO `inventoryequipment` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
try (ResultSet rs = psItem.getGeneratedKeys()) {
if (!rs.next()) {
throw new RuntimeException("Inserting item failed.");
}
if (!rs.next()) { psEquip.setInt(1, rs.getInt(1));
throw new RuntimeException("Inserting item failed."); }
Equip equip = (Equip) item;
psEquip.setInt(2, equip.getUpgradeSlots());
psEquip.setInt(3, equip.getLevel());
psEquip.setInt(4, equip.getStr());
psEquip.setInt(5, equip.getDex());
psEquip.setInt(6, equip.getInt());
psEquip.setInt(7, equip.getLuk());
psEquip.setInt(8, equip.getHp());
psEquip.setInt(9, equip.getMp());
psEquip.setInt(10, equip.getWatk());
psEquip.setInt(11, equip.getMatk());
psEquip.setInt(12, equip.getWdef());
psEquip.setInt(13, equip.getMdef());
psEquip.setInt(14, equip.getAcc());
psEquip.setInt(15, equip.getAvoid());
psEquip.setInt(16, equip.getHands());
psEquip.setInt(17, equip.getSpeed());
psEquip.setInt(18, equip.getJump());
psEquip.setInt(19, 0);
psEquip.setInt(20, equip.getVicious());
psEquip.setInt(21, equip.getItemLevel());
psEquip.setInt(22, equip.getItemExp());
psEquip.setInt(23, equip.getRingId());
psEquip.executeUpdate();
}
} }
pse.setInt(1, rs.getInt(1));
rs.close();
Equip equip = (Equip) item;
pse.setInt(2, equip.getUpgradeSlots());
pse.setInt(3, equip.getLevel());
pse.setInt(4, equip.getStr());
pse.setInt(5, equip.getDex());
pse.setInt(6, equip.getInt());
pse.setInt(7, equip.getLuk());
pse.setInt(8, equip.getHp());
pse.setInt(9, equip.getMp());
pse.setInt(10, equip.getWatk());
pse.setInt(11, equip.getMatk());
pse.setInt(12, equip.getWdef());
pse.setInt(13, equip.getMdef());
pse.setInt(14, equip.getAcc());
pse.setInt(15, equip.getAvoid());
pse.setInt(16, equip.getHands());
pse.setInt(17, equip.getSpeed());
pse.setInt(18, equip.getJump());
pse.setInt(19, 0);
pse.setInt(20, equip.getVicious());
pse.setInt(21, equip.getItemLevel());
pse.setInt(22, equip.getItemExp());
pse.setInt(23, equip.getRingId());
pse.executeUpdate();
} }
pse.close();
} }
} }
ps.close();
} finally { } finally {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (pse != null && !pse.isClosed()) {
pse.close();
}
if(rs != null && !rs.isClosed()) {
rs.close();
}
lock.unlock(); lock.unlock();
} }
} }
@@ -301,10 +266,7 @@ public enum ItemFactory {
private List<Pair<Item, MapleInventoryType>> loadItemsMerchant(int id, boolean login) throws SQLException { private List<Pair<Item, MapleInventoryType>> loadItemsMerchant(int id, boolean login) throws SQLException {
List<Pair<Item, MapleInventoryType>> items = new ArrayList<>(); List<Pair<Item, MapleInventoryType>> items = new ArrayList<>();
PreparedStatement ps = null, ps2 = null; try (Connection con = DatabaseConnection.getConnection()) {
ResultSet rs = null, rs2 = null;
Connection con = DatabaseConnection.getConnection();
try {
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
query.append("SELECT * FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `"); query.append("SELECT * FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `");
query.append(account ? "accountid" : "characterid").append("` = ?"); query.append(account ? "accountid" : "characterid").append("` = ?");
@@ -313,99 +275,78 @@ public enum ItemFactory {
query.append(" AND `inventorytype` = ").append(MapleInventoryType.EQUIPPED.getType()); query.append(" AND `inventorytype` = ").append(MapleInventoryType.EQUIPPED.getType());
} }
ps = con.prepareStatement(query.toString()); try (PreparedStatement ps = con.prepareStatement(query.toString())) {
ps.setInt(1, value); ps.setInt(1, value);
ps.setInt(2, id); ps.setInt(2, id);
rs = ps.executeQuery();
while (rs.next()) { try (ResultSet rs = ps.executeQuery()) {
ps2 = con.prepareStatement("SELECT `bundles` FROM `inventorymerchant` WHERE `inventoryitemid` = ?"); while (rs.next()) {
ps2.setInt(1, rs.getInt("inventoryitemid")); short bundles = 0;
rs2 = ps2.executeQuery(); try (PreparedStatement psBundle = con.prepareStatement("SELECT `bundles` FROM `inventorymerchant` WHERE `inventoryitemid` = ?")) {
psBundle.setInt(1, rs.getInt("inventoryitemid"));
short bundles = 0; try (ResultSet rs2 = psBundle.executeQuery()) {
if(rs2.next()) { if (rs2.next()) {
bundles = rs2.getShort("bundles"); bundles = rs2.getShort("bundles");
} }
}
MapleInventoryType mit = MapleInventoryType.getByType(rs.getByte("inventorytype"));
if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) {
items.add(new Pair<Item, MapleInventoryType>(loadEquipFromResultSet(rs), mit));
} else {
if(bundles > 0) {
int petid = rs.getInt("petid");
if (rs.wasNull()) {
petid = -1;
} }
Item item = new Item(rs.getInt("itemid"), (byte) rs.getInt("position"), (short)(bundles * rs.getInt("quantity")), petid); MapleInventoryType mit = MapleInventoryType.getByType(rs.getByte("inventorytype"));
item.setOwner(rs.getString("owner"));
item.setExpiration(rs.getLong("expiration")); if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) {
item.setGiftFrom(rs.getString("giftFrom")); items.add(new Pair<Item, MapleInventoryType>(loadEquipFromResultSet(rs), mit));
item.setFlag((short) rs.getInt("flag")); } else {
items.add(new Pair<>(item, mit)); if (bundles > 0) {
int petid = rs.getInt("petid");
if (rs.wasNull()) {
petid = -1;
}
Item item = new Item(rs.getInt("itemid"), (byte) rs.getInt("position"), (short) (bundles * rs.getInt("quantity")), petid);
item.setOwner(rs.getString("owner"));
item.setExpiration(rs.getLong("expiration"));
item.setGiftFrom(rs.getString("giftFrom"));
item.setFlag((short) rs.getInt("flag"));
items.add(new Pair<>(item, mit));
}
}
} }
} }
rs2.close();
ps2.close();
}
rs.close();
ps.close();
con.close();
} finally {
if (rs2 != null && !rs2.isClosed()) {
rs2.close();
}
if (ps2 != null && !ps2.isClosed()) {
ps2.close();
}
if (rs != null && !rs.isClosed()) {
rs.close();
}
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (con != null && !con.isClosed()) {
con.close();
} }
} }
return items; return items;
} }
private void saveItemsMerchant(List<Pair<Item, MapleInventoryType>> items, List<Short> bundlesList, int id, Connection con) throws SQLException { private void saveItemsMerchant(List<Pair<Item, MapleInventoryType>> items, List<Short> bundlesList, int id, Connection con) throws SQLException {
PreparedStatement ps = null;
PreparedStatement pse = null;
ResultSet rs = null;
Lock lock = locks[id % lockCount]; Lock lock = locks[id % lockCount];
lock.lock(); lock.lock();
try { try {
ps = con.prepareStatement("DELETE FROM `inventorymerchant` WHERE `characterid` = ?"); try (PreparedStatement ps = con.prepareStatement("DELETE FROM `inventorymerchant` WHERE `characterid` = ?")) {
ps.setInt(1, id); ps.setInt(1, id);
ps.executeUpdate(); ps.executeUpdate();
ps.close(); }
StringBuilder query = new StringBuilder(); StringBuilder query = new StringBuilder();
query.append("DELETE `inventoryitems`, `inventoryequipment` FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `"); query.append("DELETE `inventoryitems`, `inventoryequipment` FROM `inventoryitems` LEFT JOIN `inventoryequipment` USING(`inventoryitemid`) WHERE `type` = ? AND `");
query.append(account ? "accountid" : "characterid").append("` = ?"); query.append(account ? "accountid" : "characterid").append("` = ?");
ps = con.prepareStatement(query.toString());
ps.setInt(1, value);
ps.setInt(2, id);
ps.executeUpdate();
ps.close();
ps = con.prepareStatement("INSERT INTO `inventoryitems` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
if (!items.isEmpty()) { try (PreparedStatement ps = con.prepareStatement(query.toString())) {
int i = 0; ps.setInt(1, value);
for (Pair<Item, MapleInventoryType> pair : items) { ps.setInt(2, id);
Item item = pair.getLeft(); ps.executeUpdate();
Short bundles = bundlesList.get(i); }
MapleInventoryType mit = pair.getRight();
i++;
int i = 0;
for (Pair<Item, MapleInventoryType> pair : items) {
final Item item = pair.getLeft();
final Short bundles = bundlesList.get(i);
final MapleInventoryType mit = pair.getRight();
i++;
final int genKey;
// Item
try (PreparedStatement ps = con.prepareStatement("INSERT INTO `inventoryitems` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
ps.setInt(1, value); ps.setInt(1, value);
ps.setString(2, account ? null : String.valueOf(id)); ps.setString(2, account ? null : String.valueOf(id));
ps.setString(3, account ? String.valueOf(id) : null); ps.setString(3, account ? String.valueOf(id) : null);
@@ -420,67 +361,56 @@ public enum ItemFactory {
ps.setString(12, item.getGiftFrom()); ps.setString(12, item.getGiftFrom());
ps.executeUpdate(); ps.executeUpdate();
rs = ps.getGeneratedKeys(); try (ResultSet rs = ps.getGeneratedKeys()) {
if (!rs.next()) { if (!rs.next()) {
throw new RuntimeException("Inserting item failed."); throw new RuntimeException("Inserting item failed.");
}
genKey = rs.getInt(1);
} }
}
int genKey = rs.getInt(1); // Merchant
rs.close(); try (PreparedStatement ps = con.prepareStatement("INSERT INTO `inventorymerchant` VALUES (DEFAULT, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS)) {
ps.setInt(1, genKey);
ps.setInt(2, id);
ps.setInt(3, bundles);
ps.executeUpdate();
}
pse = con.prepareStatement("INSERT INTO `inventorymerchant` VALUES (DEFAULT, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS); // Equipment
pse.setInt(1, genKey); if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) {
pse.setInt(2, id); try (PreparedStatement ps = con.prepareStatement("INSERT INTO `inventoryequipment` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
pse.setInt(3, bundles); ps.setInt(1, genKey);
pse.executeUpdate();
pse.close();
if (mit.equals(MapleInventoryType.EQUIP) || mit.equals(MapleInventoryType.EQUIPPED)) {
pse = con.prepareStatement("INSERT INTO `inventoryequipment` VALUES (DEFAULT, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
pse.setInt(1, genKey);
Equip equip = (Equip) item; Equip equip = (Equip) item;
pse.setInt(2, equip.getUpgradeSlots()); ps.setInt(2, equip.getUpgradeSlots());
pse.setInt(3, equip.getLevel()); ps.setInt(3, equip.getLevel());
pse.setInt(4, equip.getStr()); ps.setInt(4, equip.getStr());
pse.setInt(5, equip.getDex()); ps.setInt(5, equip.getDex());
pse.setInt(6, equip.getInt()); ps.setInt(6, equip.getInt());
pse.setInt(7, equip.getLuk()); ps.setInt(7, equip.getLuk());
pse.setInt(8, equip.getHp()); ps.setInt(8, equip.getHp());
pse.setInt(9, equip.getMp()); ps.setInt(9, equip.getMp());
pse.setInt(10, equip.getWatk()); ps.setInt(10, equip.getWatk());
pse.setInt(11, equip.getMatk()); ps.setInt(11, equip.getMatk());
pse.setInt(12, equip.getWdef()); ps.setInt(12, equip.getWdef());
pse.setInt(13, equip.getMdef()); ps.setInt(13, equip.getMdef());
pse.setInt(14, equip.getAcc()); ps.setInt(14, equip.getAcc());
pse.setInt(15, equip.getAvoid()); ps.setInt(15, equip.getAvoid());
pse.setInt(16, equip.getHands()); ps.setInt(16, equip.getHands());
pse.setInt(17, equip.getSpeed()); ps.setInt(17, equip.getSpeed());
pse.setInt(18, equip.getJump()); ps.setInt(18, equip.getJump());
pse.setInt(19, 0); ps.setInt(19, 0);
pse.setInt(20, equip.getVicious()); ps.setInt(20, equip.getVicious());
pse.setInt(21, equip.getItemLevel()); ps.setInt(21, equip.getItemLevel());
pse.setInt(22, equip.getItemExp()); ps.setInt(22, equip.getItemExp());
pse.setInt(23, equip.getRingId()); ps.setInt(23, equip.getRingId());
pse.executeUpdate(); ps.executeUpdate();
pse.close();
} }
} }
} }
ps.close();
} finally { } finally {
if (ps != null && !ps.isClosed()) {
ps.close();
}
if (pse != null && !pse.isClosed()) {
pse.close();
}
if(rs != null && !rs.isClosed()) {
rs.close();
}
lock.unlock(); lock.unlock();
} }
} }