From 2af5251a85e35653adb39bca9c92f97795ba1dae Mon Sep 17 00:00:00 2001 From: ceze88 <70201650+ceze88@users.noreply.github.com> Date: Sun, 14 Aug 2022 11:29:25 +0200 Subject: [PATCH] Remove deprecated methods, fix MySQL --- .../songoda/ultimatekits/UltimateKits.java | 3 +- .../ultimatekits/database/DataManager.java | 101 ++++++++++-------- .../ultimatekits/settings/Settings.java | 1 + 3 files changed, 60 insertions(+), 45 deletions(-) diff --git a/src/main/java/com/songoda/ultimatekits/UltimateKits.java b/src/main/java/com/songoda/ultimatekits/UltimateKits.java index 40c18ee..d3e9019 100644 --- a/src/main/java/com/songoda/ultimatekits/UltimateKits.java +++ b/src/main/java/com/songoda/ultimatekits/UltimateKits.java @@ -166,8 +166,9 @@ public class UltimateKits extends SongodaPlugin { String username = Settings.MYSQL_USERNAME.getString(); String password = Settings.MYSQL_PASSWORD.getString(); boolean useSSL = Settings.MYSQL_USE_SSL.getBoolean(); + int poolSize = Settings.MYSQL_POOL_SIZE.getInt(); - this.databaseConnector = new MySQLConnector(this, hostname, port, database, username, password, useSSL); + this.databaseConnector = new MySQLConnector(this, hostname, port, database, username, password, useSSL, poolSize); this.getLogger().info("Data handler connected using MySQL."); } else { this.databaseConnector = new SQLiteConnector(this); diff --git a/src/main/java/com/songoda/ultimatekits/database/DataManager.java b/src/main/java/com/songoda/ultimatekits/database/DataManager.java index cc8a78d..b488c02 100644 --- a/src/main/java/com/songoda/ultimatekits/database/DataManager.java +++ b/src/main/java/com/songoda/ultimatekits/database/DataManager.java @@ -11,6 +11,7 @@ import org.bukkit.Location; import org.bukkit.World; import org.bukkit.plugin.Plugin; +import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Statement; @@ -25,38 +26,40 @@ public class DataManager extends DataManagerAbstract { } public void bulkUpdateBlockData(Map blockData) { - this.databaseConnector.connect(connection -> { + try (Connection connection = this.databaseConnector.getConnection()) { String updateData = "UPDATE " + this.getTablePrefix() + "blockdata SET type = ?, kit = ?, holograms = ?, " + "displayItems = ?, particles = ?, itemOverride = ? " + "WHERE world = ? AND x = ? AND y = ? AND z = ?"; - try (PreparedStatement statement = connection.prepareStatement(updateData)) { - for (KitBlockData data : blockData.values()) { - if (data == null || data.getWorld() == null) continue; - statement.setString(1, data.getType().toString()); - statement.setString(2, data.getKit().getKey()); - statement.setBoolean(3, data.showHologram()); - statement.setBoolean(4, data.isDisplayingItems()); - statement.setBoolean(5, data.hasParticles()); - statement.setBoolean(6, data.isItemOverride()); - statement.setString(7, data.getWorld().getName()); - statement.setInt(8, data.getX()); - statement.setInt(9, data.getY()); - statement.setInt(10, data.getZ()); - statement.addBatch(); - } - - statement.executeBatch(); + PreparedStatement statement = connection.prepareStatement(updateData); + for (KitBlockData data : blockData.values()) { + if (data == null || data.getWorld() == null) continue; + statement.setString(1, data.getType().toString()); + statement.setString(2, data.getKit().getKey()); + statement.setBoolean(3, data.showHologram()); + statement.setBoolean(4, data.isDisplayingItems()); + statement.setBoolean(5, data.hasParticles()); + statement.setBoolean(6, data.isItemOverride()); + statement.setString(7, data.getWorld().getName()); + statement.setInt(8, data.getX()); + statement.setInt(9, data.getY()); + statement.setInt(10, data.getZ()); + statement.addBatch(); } - }); + + statement.executeBatch(); + } catch (Exception ex) { + ex.printStackTrace(); + } } public void updateBlockData(KitBlockData blockData) { if (blockData.getWorld() == null) return; - this.async(() -> this.databaseConnector.connect(connection -> { - String updateData = "UPDATE " + this.getTablePrefix() + "blockdata SET type = ?, kit = ?, holograms = ?, " + - "displayItems = ?, particles = ?, itemOverride = ? " + - "WHERE world = ? AND x = ? AND y = ? AND z = ?"; - try (PreparedStatement statement = connection.prepareStatement(updateData)) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String updateData = "UPDATE " + this.getTablePrefix() + "blockdata SET type = ?, kit = ?, holograms = ?, " + + "displayItems = ?, particles = ?, itemOverride = ? " + + "WHERE world = ? AND x = ? AND y = ? AND z = ?"; + PreparedStatement statement = connection.prepareStatement(updateData); statement.setString(1, blockData.getType().toString()); statement.setString(2, blockData.getKit().getKey()); statement.setBoolean(3, blockData.showHologram()); @@ -68,17 +71,20 @@ public class DataManager extends DataManagerAbstract { statement.setInt(9, blockData.getY()); statement.setInt(10, blockData.getZ()); statement.executeUpdate(); + } catch (Exception ex) { + ex.printStackTrace(); } - })); + }); } public void createBlockData(KitBlockData blockData) { if (blockData.getWorld() == null) return; - this.async(() -> this.databaseConnector.connect(connection -> { - String createData = "INSERT INTO " + this.getTablePrefix() + "blockdata (" + - "type, kit, holograms, displayItems, particles, itemOverride, world, x, y, z)" + - "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; - try (PreparedStatement statement = connection.prepareStatement(createData)) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String createData = "INSERT INTO " + this.getTablePrefix() + "blockdata (" + + "type, kit, holograms, displayItems, particles, itemOverride, world, x, y, z)" + + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + PreparedStatement statement = connection.prepareStatement(createData); statement.setString(1, blockData.getType().toString()); statement.setString(2, blockData.getKit().getKey()); statement.setBoolean(3, blockData.showHologram()); @@ -90,29 +96,35 @@ public class DataManager extends DataManagerAbstract { statement.setInt(9, blockData.getY()); statement.setInt(10, blockData.getZ()); statement.executeUpdate(); + } catch (Exception ex) { + ex.printStackTrace(); } - })); + }); } public void deleteBlockData(KitBlockData blockData) { - this.async(() -> this.databaseConnector.connect(connection -> { - String deleteData = "DELETE FROM " + this.getTablePrefix() + "blockdata WHERE world = ? " + - "AND x = ? AND y = ? AND z = ?"; - try (PreparedStatement statement = connection.prepareStatement(deleteData)) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String deleteData = "DELETE FROM " + this.getTablePrefix() + "blockdata WHERE world = ? " + + "AND x = ? AND y = ? AND z = ?"; + PreparedStatement statement = connection.prepareStatement(deleteData); statement.setString(1, blockData.getWorld().getName()); statement.setInt(2, blockData.getX()); statement.setInt(3, blockData.getY()); statement.setInt(4, blockData.getZ()); statement.executeUpdate(); + } catch (Exception ex) { + ex.printStackTrace(); } - })); + }); } public void getBlockData(Consumer> callback) { - this.async(() -> this.databaseConnector.connect(connection -> { - String selectData = "SELECT * FROM " + this.getTablePrefix() + "blockdata"; - Map blockData = new HashMap<>(); - try (Statement statement = connection.createStatement()) { + this.runAsync(() -> { + try (Connection connection = this.databaseConnector.getConnection()) { + String selectData = "SELECT * FROM " + this.getTablePrefix() + "blockdata"; + Map blockData = new HashMap<>(); + Statement statement = connection.createStatement(); ResultSet result = statement.executeQuery(selectData); while (result.next()) { @@ -134,10 +146,11 @@ public class DataManager extends DataManagerAbstract { blockData.put(location, new KitBlockData(kit, location, type, holograms, particles, displayItems, itemOverride)); } - result.close(); - } - this.sync(() -> callback.accept(blockData)); - })); + this.sync(() -> callback.accept(blockData)); + } catch (Exception ex) { + ex.printStackTrace(); + } + }); } } diff --git a/src/main/java/com/songoda/ultimatekits/settings/Settings.java b/src/main/java/com/songoda/ultimatekits/settings/Settings.java index 43671c0..8cdab91 100644 --- a/src/main/java/com/songoda/ultimatekits/settings/Settings.java +++ b/src/main/java/com/songoda/ultimatekits/settings/Settings.java @@ -51,6 +51,7 @@ public class Settings { public static final ConfigSetting MYSQL_USERNAME = new ConfigSetting(config, "MySQL.Username", "user"); public static final ConfigSetting MYSQL_PASSWORD = new ConfigSetting(config, "MySQL.Password", "pass"); public static final ConfigSetting MYSQL_USE_SSL = new ConfigSetting(config, "MySQL.Use SSL", false); + public static final ConfigSetting MYSQL_POOL_SIZE = new ConfigSetting(config, "MySQL.Pool Size", 3, "Determines the number of connections the pool is using. Increase this value if you are getting timeout errors when more players online."); public static final ConfigSetting PARTICLE_AMOUNT = new ConfigSetting(config, "data.particlesettings.amount", 25); public static final ConfigSetting PARTICLE_TYPE = new ConfigSetting(config, "data.particlesettings.type", "SPELL_WITCH");