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