mirror of
https://github.com/HibiscusMC/HMCCosmetics.git
synced 2025-12-19 15:09:19 +00:00
fix: fix possible database memory leak (as well as improvements in code)
This commit is contained in:
@@ -39,10 +39,12 @@ public class MySQLData extends SQLData {
|
||||
try {
|
||||
openConnection();
|
||||
if (connection == null) throw new NullPointerException("Connection is null");
|
||||
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
|
||||
"(UUID varchar(36) PRIMARY KEY, " +
|
||||
"COSMETICS MEDIUMTEXT " +
|
||||
");").execute();
|
||||
");")) {
|
||||
preparedStatement.execute();
|
||||
}
|
||||
} catch (SQLException | NullPointerException e) {
|
||||
plugin.getLogger().severe("");
|
||||
plugin.getLogger().severe("");
|
||||
@@ -60,17 +62,11 @@ public class MySQLData extends SQLData {
|
||||
@Override
|
||||
public void clear(UUID uniqueId) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
PreparedStatement preparedSt = null;
|
||||
try {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedSt != null) preparedSt.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -23,22 +23,17 @@ public abstract class SQLData extends Data {
|
||||
return CompletableFuture.supplyAsync(() -> {
|
||||
UserData data = new UserData(uniqueId);
|
||||
|
||||
PreparedStatement preparedStatement = null;
|
||||
try {
|
||||
preparedStatement = preparedStatement("SELECT * FROM COSMETICDATABASE WHERE UUID = ?;");
|
||||
try (PreparedStatement preparedStatement = preparedStatement("SELECT * FROM COSMETICDATABASE WHERE UUID = ?;")){
|
||||
preparedStatement.setString(1, uniqueId.toString());
|
||||
ResultSet rs = preparedStatement.executeQuery();
|
||||
if (rs.next()) {
|
||||
String rawData = rs.getString("COSMETICS");
|
||||
HashMap<CosmeticSlot, Map.Entry<Cosmetic, Integer>> cosmetics = deserializeData(rawData);
|
||||
data.setCosmetics(cosmetics);
|
||||
try (ResultSet rs = preparedStatement.executeQuery()) {
|
||||
if (rs.next()) {
|
||||
String rawData = rs.getString("COSMETICS");
|
||||
HashMap<CosmeticSlot, Map.Entry<Cosmetic, Integer>> cosmetics = deserializeData(rawData);
|
||||
data.setCosmetics(cosmetics);
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedStatement != null) preparedStatement.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
return data;
|
||||
});
|
||||
@@ -48,18 +43,12 @@ public abstract class SQLData extends Data {
|
||||
@SuppressWarnings("resource")
|
||||
public void save(CosmeticUser user) {
|
||||
Runnable run = () -> {
|
||||
PreparedStatement preparedSt = null;
|
||||
try {
|
||||
preparedSt = preparedStatement("REPLACE INTO COSMETICDATABASE(UUID,COSMETICS) VALUES(?,?);");
|
||||
try (PreparedStatement preparedSt = preparedStatement("REPLACE INTO COSMETICDATABASE(UUID,COSMETICS) VALUES(?,?);")) {
|
||||
preparedSt.setString(1, user.getUniqueId().toString());
|
||||
preparedSt.setString(2, serializeData(user));
|
||||
preparedSt.executeUpdate();
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
try {
|
||||
if (preparedSt != null) preparedSt.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
};
|
||||
if (!HMCCosmeticsPlugin.getInstance().isDisabled()) {
|
||||
|
||||
@@ -35,10 +35,12 @@ public class SQLiteData extends SQLData {
|
||||
connection = DriverManager.getConnection("jdbc:sqlite:" + dataFolder);
|
||||
|
||||
openConnection();
|
||||
connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
|
||||
try (PreparedStatement preparedStatement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS `COSMETICDATABASE` " +
|
||||
"(UUID varchar(36) PRIMARY KEY, " +
|
||||
"COSMETICS MEDIUMTEXT " +
|
||||
");").execute();
|
||||
");")) {
|
||||
preparedStatement.execute();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
@@ -48,17 +50,11 @@ public class SQLiteData extends SQLData {
|
||||
@SuppressWarnings("resource")
|
||||
public void clear(UUID uniqueId) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(HMCCosmeticsPlugin.getInstance(), () -> {
|
||||
PreparedStatement preparedSt = null;
|
||||
try {
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
try {
|
||||
if (preparedSt != null) preparedSt.close();
|
||||
} catch (SQLException e) {}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user