From 2e66d305d4c926a08210ef4034b5b273266f2088 Mon Sep 17 00:00:00 2001 From: Beny Date: Thu, 23 May 2019 23:57:22 +0100 Subject: [PATCH] Possible database leaks fixed --- .../habbohotel/catalog/CatalogManager.java | 61 ++++++++++--------- .../habbo/habbohotel/catalog/CatalogPage.java | 4 ++ .../catalog/layouts/CatalogRootLayout.java | 5 ++ .../catalog/marketplace/MarketPlace.java | 32 +++++----- .../catalog/marketplace/MarketPlaceOffer.java | 6 +- .../habbohotel/guilds/forums/ForumThread.java | 24 ++++---- .../guilds/forums/ForumThreadComment.java | 9 +-- .../habbohotel/modtool/ModToolManager.java | 24 ++++++-- .../eu/habbo/habbohotel/pets/PetManager.java | 44 +++++++++++-- .../com/eu/habbo/habbohotel/rooms/Room.java | 42 ++++++++----- .../guilds/forums/GuildForumDataComposer.java | 2 +- .../guilds/forums/GuildForumListComposer.java | 6 +- 12 files changed, 166 insertions(+), 93 deletions(-) diff --git a/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogManager.java b/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogManager.java index edaedd7e..6acfe1c3 100644 --- a/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogManager.java @@ -212,27 +212,20 @@ public class CatalogManager { Emulator.getPluginManager().fireEvent(new EmulatorLoadCatalogManagerEvent()); - try - { - this.loadLimitedNumbers(); - this.loadCatalogPages(); - this.loadCatalogFeaturedPages(); - this.loadCatalogItems(); - this.loadClubOffers(); - this.loadTargetOffers(); - this.loadVouchers(); - this.loadClothing(); - this.loadRecycler(); - this.loadGiftWrappers(); - this.loadCalendarRewards(); - } - catch(SQLException e) - { - Emulator.getLogging().logSQLException(e); - } + this.loadLimitedNumbers(); + this.loadCatalogPages(); + this.loadCatalogFeaturedPages(); + this.loadCatalogItems(); + this.loadClubOffers(); + this.loadTargetOffers(); + this.loadVouchers(); + this.loadClothing(); + this.loadRecycler(); + this.loadGiftWrappers(); + this.loadCalendarRewards(); } - private synchronized void loadLimitedNumbers() throws SQLException + private synchronized void loadLimitedNumbers() { this.limitedNumbers.clear(); @@ -270,12 +263,12 @@ public class CatalogManager } - private synchronized void loadCatalogPages() throws SQLException + private synchronized void loadCatalogPages() { this.catalogPages.clear(); final THashMap pages = new THashMap<>(); - pages.put(-1, new CatalogRootLayout(null)); + pages.put(-1, new CatalogRootLayout()); try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM catalog_pages ORDER BY parent_id, id")) { try (ResultSet set = statement.executeQuery()) @@ -338,7 +331,7 @@ public class CatalogManager } - private synchronized void loadCatalogFeaturedPages() throws SQLException + private synchronized void loadCatalogFeaturedPages() { this.catalogFeaturedPages.clear(); @@ -364,7 +357,7 @@ public class CatalogManager } } - private synchronized void loadCatalogItems() throws SQLException + private synchronized void loadCatalogItems() { this.clubItems.clear(); catalogItemAmount = 0; @@ -431,7 +424,7 @@ public class CatalogManager } } - private void loadClubOffers() throws SQLException + private void loadClubOffers() { this.clubOffers.clear(); @@ -446,9 +439,13 @@ public class CatalogManager } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private void loadTargetOffers() throws SQLException + private void loadTargetOffers() { synchronized (this.targetOffers) { @@ -465,11 +462,15 @@ public class CatalogManager } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } } - private void loadVouchers() throws SQLException + private void loadVouchers() { synchronized (this.vouchers) { @@ -482,11 +483,15 @@ public class CatalogManager this.vouchers.add(new Voucher(set)); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } } - public void loadRecycler() throws SQLException + public void loadRecycler() { synchronized (this.prizes) { @@ -520,7 +525,7 @@ public class CatalogManager } - public void loadGiftWrappers() throws SQLException + public void loadGiftWrappers() { synchronized (this.giftWrappers) { diff --git a/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogPage.java b/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogPage.java index 640b74a4..c0e847ea 100644 --- a/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogPage.java +++ b/src/main/java/com/eu/habbo/habbohotel/catalog/CatalogPage.java @@ -39,6 +39,10 @@ public abstract class CatalogPage implements Comparable, ISerialize private final TIntObjectMap catalogItems = TCollections.synchronizedMap(new TIntObjectHashMap<>()); private final ArrayList included = new ArrayList<>(); + public CatalogPage() + { + } + public CatalogPage(ResultSet set) throws SQLException { if (set == null) diff --git a/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/CatalogRootLayout.java b/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/CatalogRootLayout.java index c303ef78..02c39bee 100644 --- a/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/CatalogRootLayout.java +++ b/src/main/java/com/eu/habbo/habbohotel/catalog/layouts/CatalogRootLayout.java @@ -8,6 +8,11 @@ import java.sql.SQLException; public class CatalogRootLayout extends CatalogPage { + public CatalogRootLayout() + { + super(); + } + public CatalogRootLayout(ResultSet set) throws SQLException { super(null); diff --git a/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlace.java b/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlace.java index 92a79c4d..7660b824 100644 --- a/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlace.java +++ b/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlace.java @@ -368,7 +368,7 @@ public class MarketPlace } - public static void sendErrorMessage(GameClient client, int baseItemId, int offerId) throws SQLException + public static void sendErrorMessage(GameClient client, int baseItemId, int offerId) { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT marketplace_items.*, COUNT( * ) AS count\n" + "FROM marketplace_items\n" + @@ -394,6 +394,10 @@ public class MarketPlace } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } @@ -412,24 +416,18 @@ public class MarketPlace } RequestOffersEvent.cachedResults.clear(); - try - { - client.sendResponse(new RemoveHabboItemComposer(event.item.getGiftAdjustedId())); - client.sendResponse(new InventoryRefreshComposer()); - event.item.setFromGift(false); + client.sendResponse(new RemoveHabboItemComposer(event.item.getGiftAdjustedId())); + client.sendResponse(new InventoryRefreshComposer()); - MarketPlaceOffer offer = new MarketPlaceOffer(event.item, event.price, client.getHabbo()); - client.getHabbo().getInventory().addMarketplaceOffer(offer); - client.getHabbo().getInventory().getItemsComponent().removeHabboItem(event.item); - item.setUserId(-1); - item.needsUpdate(true); - Emulator.getThreading().run(item); - } - catch (SQLException e) - { - Emulator.getLogging().logSQLException(e); - } + event.item.setFromGift(false); + + MarketPlaceOffer offer = new MarketPlaceOffer(event.item, event.price, client.getHabbo()); + client.getHabbo().getInventory().addMarketplaceOffer(offer); + client.getHabbo().getInventory().getItemsComponent().removeHabboItem(event.item); + item.setUserId(-1); + item.needsUpdate(true); + Emulator.getThreading().run(item); return true; } diff --git a/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlaceOffer.java b/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlaceOffer.java index 52c87d58..62985faa 100644 --- a/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlaceOffer.java +++ b/src/main/java/com/eu/habbo/habbohotel/catalog/marketplace/MarketPlaceOffer.java @@ -48,7 +48,7 @@ public class MarketPlaceOffer implements Runnable } } - public MarketPlaceOffer(HabboItem item, int price, Habbo habbo) throws SQLException + public MarketPlaceOffer(HabboItem item, int price, Habbo habbo) { this.price = price; this.baseItem = item.getBaseItem(); @@ -76,6 +76,10 @@ public class MarketPlaceOffer implements Runnable } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } public int getOfferId() diff --git a/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThread.java b/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThread.java index 4b54271a..0a308002 100644 --- a/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThread.java +++ b/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThread.java @@ -349,7 +349,7 @@ public class ForumThread implements Runnable, ISerialize { return createdThread; } - public static THashSet getByGuildId(int guildId) throws SQLException { + public static THashSet getByGuildId(int guildId) { THashSet threads = null; if(guildThreadsCache.containsKey(guildId)) { @@ -381,14 +381,15 @@ public class ForumThread implements Runnable, ISerialize { )) { statement.setInt(1, guildId); - ResultSet set = statement.executeQuery(); - while(set.next()) { - ForumThread thread = new ForumThread(set); - synchronized (threads) { - threads.add(thread); + try(ResultSet set = statement.executeQuery()) { + while (set.next()) { + ForumThread thread = new ForumThread(set); + synchronized (threads) { + threads.add(thread); + } + cacheThread(thread); } - cacheThread(thread); } } catch (SQLException e) @@ -425,11 +426,12 @@ public class ForumThread implements Runnable, ISerialize { )) { statement.setInt(1, threadId); - ResultSet set = statement.executeQuery(); - while(set.next()) { - foundThread = new ForumThread(set); - cacheThread(foundThread); + try(ResultSet set = statement.executeQuery()) { + while (set.next()) { + foundThread = new ForumThread(set); + cacheThread(foundThread); + } } } catch (SQLException e) diff --git a/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThreadComment.java b/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThreadComment.java index 4db3e0fa..1e320d5f 100644 --- a/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThreadComment.java +++ b/src/main/java/com/eu/habbo/habbohotel/guilds/forums/ForumThreadComment.java @@ -57,11 +57,12 @@ public class ForumThreadComment implements Runnable, ISerialize { try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM `guilds_forums_comments` WHERE `id` = ? LIMIT 1")) { statement.setInt(1, id); - ResultSet set = statement.executeQuery(); - while(set.next()) { - foundComment = new ForumThreadComment(set); - cacheComment(foundComment); + try(ResultSet set = statement.executeQuery()) { + while (set.next()) { + foundComment = new ForumThreadComment(set); + cacheComment(foundComment); + } } } catch (SQLException e) diff --git a/src/main/java/com/eu/habbo/habbohotel/modtool/ModToolManager.java b/src/main/java/com/eu/habbo/habbohotel/modtool/ModToolManager.java index 2ef67048..081d35b1 100644 --- a/src/main/java/com/eu/habbo/habbohotel/modtool/ModToolManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/modtool/ModToolManager.java @@ -68,7 +68,7 @@ public class ModToolManager } } - private void loadCategory(Connection connection) throws SQLException + private void loadCategory(Connection connection) { try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM support_issue_categories")) { @@ -89,9 +89,13 @@ public class ModToolManager } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private void loadPresets(Connection connection) throws SQLException + private void loadPresets(Connection connection) { synchronized (this.presets) { @@ -102,10 +106,14 @@ public class ModToolManager this.presets.get(set.getString("type")).add(set.getString("preset")); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } } - private void loadTickets(Connection connection) throws SQLException + private void loadTickets(Connection connection) { synchronized (this.tickets) { @@ -116,10 +124,14 @@ public class ModToolManager this.tickets.put(set.getInt("id"), new ModToolIssue(set)); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } } - private void loadCfhCategories(Connection connection) throws SQLException + private void loadCfhCategories(Connection connection) { try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT " + "support_cfh_topics.id, " + @@ -144,6 +156,10 @@ public class ModToolManager this.cfhCategories.get(set.getInt("support_cfh_category_id")).addTopic(new CfhTopic(set, this.getIssuePreset(set.getInt("default_sanction")))); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } public CfhTopic getCfhTopic(int topicId) diff --git a/src/main/java/com/eu/habbo/habbohotel/pets/PetManager.java b/src/main/java/com/eu/habbo/habbohotel/pets/PetManager.java index 0125b947..a8564f34 100644 --- a/src/main/java/com/eu/habbo/habbohotel/pets/PetManager.java +++ b/src/main/java/com/eu/habbo/habbohotel/pets/PetManager.java @@ -106,7 +106,7 @@ public class PetManager } } - private void loadRaces(Connection connection) throws SQLException + private void loadRaces(Connection connection) { this.petRaces.clear(); @@ -120,9 +120,13 @@ public class PetManager this.petRaces.get(set.getInt("race")).add(new PetRace(set)); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private void loadPetData(Connection connection) throws SQLException + private void loadPetData(Connection connection) { try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_actions ORDER BY pet_type ASC")) { @@ -131,13 +135,17 @@ public class PetManager this.petData.put(set.getInt("pet_type"), new PetData(set)); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } this.loadPetItems(connection); this.loadPetVocals(connection); } - private void loadPetItems(Connection connection) throws SQLException + private void loadPetItems(Connection connection) { try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_items")) { @@ -169,9 +177,13 @@ public class PetManager } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private void loadPetVocals(Connection connection) throws SQLException + private void loadPetVocals(Connection connection) { try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_vocals")) { @@ -206,9 +218,13 @@ public class PetManager } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private void loadPetCommands(Connection connection) throws SQLException + private void loadPetCommands(Connection connection) { THashMap commandsList = new THashMap<>(); try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_commands_data")) @@ -218,6 +234,10 @@ public class PetManager commandsList.put(set.getInt("command_id"), new PetCommand(set, this.petActions.get(set.getInt("command_id")))); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_commands ORDER BY pet_id ASC")) { @@ -231,9 +251,13 @@ public class PetManager } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private void loadPetBreeding(Connection connection) throws SQLException + private void loadPetBreeding(Connection connection) { try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_breeding")) { @@ -242,6 +266,10 @@ public class PetManager this.breedingPetType.put(set.getInt("pet_id"), set.getInt("offspring_id")); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } try (Statement statement = connection.createStatement(); ResultSet set = statement.executeQuery("SELECT * FROM pet_breeding_races")) { @@ -261,6 +289,10 @@ public class PetManager this.breedingReward.get(reward.petType).get(reward.rarityLevel).add(reward); } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } public THashSet getBreeds(String petName) diff --git a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java index 0aa1fd87..e1a292db 100644 --- a/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java +++ b/src/main/java/com/eu/habbo/habbohotel/rooms/Room.java @@ -242,17 +242,6 @@ public class Room implements Comparable, ISerialize, Runnable this.bannedHabbos = new TIntObjectHashMap<>(); - - - - - - - - - - - try (Connection connection = Emulator.getDatabase().getDataSource().getConnection(); PreparedStatement statement = connection.prepareStatement("SELECT * FROM room_promotions WHERE room_id = ? AND end_timestamp > ? LIMIT 1")) { if(this.promoted) @@ -273,6 +262,11 @@ public class Room implements Comparable, ISerialize, Runnable this.loadBans(connection); } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } + this.tradeMode = set.getInt("trade_mode"); this.moveDiagonally = set.getString("move_diagonally").equals("1"); @@ -456,7 +450,7 @@ public class Room implements Comparable, ISerialize, Runnable } } - private synchronized void loadItems(Connection connection) throws SQLException + private synchronized void loadItems(Connection connection) { this.roomItems.clear(); @@ -471,9 +465,13 @@ public class Room implements Comparable, ISerialize, Runnable } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private synchronized void loadWiredData(Connection connection) throws SQLException + private synchronized void loadWiredData(Connection connection) { try (PreparedStatement statement = connection.prepareStatement("SELECT id, wired_data FROM items WHERE room_id = ? AND wired_data<>''")) { @@ -499,13 +497,17 @@ public class Room implements Comparable, ISerialize, Runnable } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } catch (Exception e) { Emulator.getLogging().logErrorLine(e); } } - private synchronized void loadBots(Connection connection) throws SQLException + private synchronized void loadBots(Connection connection) { this.currentBots.clear(); @@ -550,7 +552,7 @@ public class Room implements Comparable, ISerialize, Runnable } } - private synchronized void loadPets(Connection connection) throws SQLException + private synchronized void loadPets(Connection connection) { this.currentPets.clear(); @@ -591,9 +593,13 @@ public class Room implements Comparable, ISerialize, Runnable } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } - private synchronized void loadWordFilter(Connection connection) throws SQLException + private synchronized void loadWordFilter(Connection connection) { this.wordFilterWords.clear(); @@ -608,6 +614,10 @@ public class Room implements Comparable, ISerialize, Runnable } } } + catch (SQLException e) + { + Emulator.getLogging().logSQLException(e); + } } public void updateTile(RoomTile tile) diff --git a/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumDataComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumDataComposer.java index 7fb4d878..fc8370e2 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumDataComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumDataComposer.java @@ -93,7 +93,7 @@ public class GuildForumDataComposer extends MessageComposer { return this.response; } - public static void serializeForumData(ServerMessage response, Guild guild, Habbo habbo) throws SQLException { + public static void serializeForumData(ServerMessage response, Guild guild, Habbo habbo) { final THashSet forumThreads = ForumThread.getByGuildId(guild.getId()); int lastSeenAt = 0; diff --git a/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumListComposer.java b/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumListComposer.java index c885547d..a4d7a8ae 100644 --- a/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumListComposer.java +++ b/src/main/java/com/eu/habbo/messages/outgoing/guilds/forums/GuildForumListComposer.java @@ -47,11 +47,7 @@ public class GuildForumListComposer extends MessageComposer { if(!it.hasNext()) break; - try { - GuildForumDataComposer.serializeForumData(this.response, it.next(), habbo); - } catch (SQLException e) { - return new ConnectionErrorComposer(500).compose(); - } + GuildForumDataComposer.serializeForumData(this.response, it.next(), habbo); } return this.response;