From 2bf52808273047c81e72d2f21063aece723649ba Mon Sep 17 00:00:00 2001 From: Craftinators Date: Mon, 27 Feb 2023 15:29:50 -0500 Subject: [PATCH] fix: reorder null checks --- .../hmccosmetics/database/types/MySQLData.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java index 4abbf48b..4911257d 100644 --- a/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java +++ b/common/src/main/java/com/hibiscusmc/hmccosmetics/database/types/MySQLData.java @@ -52,8 +52,7 @@ public class MySQLData extends SQLData { @Override public void clear(UUID uniqueId) { Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> { - try { - PreparedStatement preparedSt = preparedStatement("DELETE FROM COSMETICDATABASE WHERE UUID=?;"); + try (PreparedStatement preparedSt = preparedStatement("DELETE FROM COSMETICDATABASE WHERE UUID=?;")) { preparedSt.setString(1, uniqueId.toString()); preparedSt.executeUpdate(); } catch (SQLException e) { @@ -70,11 +69,13 @@ public class MySQLData extends SQLData { // }); // connection = DriverManager.getConnection("jdbc:mysql://" + DatabaseSettings.getHost() + ":" + DatabaseSettings.getPort() + "/" + DatabaseSettings.getDatabase(), setupProperties()); - if (connection != null && !connection.isClosed()) return; - - // Close connection if still active - if (connection != null) { - close(); + // Connection isn't null AND Connection isn't closed :: return + try { + if (isConnectionOpen()) { + return; + } else close(); // Close connection if still active + } catch (RuntimeException e) { + e.printStackTrace(); // If isConnectionOpen() throws error } // Connect to database host @@ -107,7 +108,7 @@ public class MySQLData extends SQLData { return props; } - private boolean isConnectionOpen() { + private boolean isConnectionOpen() throws RuntimeException { try { return connection != null && !connection.isClosed(); } catch (SQLException e) {