Skip to content

Commit

Permalink
fix a potential memory leak in playerdb
Browse files Browse the repository at this point in the history
  • Loading branch information
obfuscatedgenerated committed May 27, 2023
1 parent 187b53d commit 4dedc10
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 40 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>codes.ollieg</groupId>
<artifactId>MagicMOTD</artifactId>
<version>1.0.2</version>
<version>1.0.3</version>
<packaging>jar</packaging>

<name>MagicMOTD</name>
Expand Down
96 changes: 57 additions & 39 deletions src/main/java/codes/ollieg/magicmotd/PlayerDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public class PlayerDB {

/**
* Constructs a new {@link PlayerDB}.
*
* @param plugin the plugin
* @throws IllegalArgumentException if the plugin is null
*/
Expand All @@ -34,7 +35,8 @@ public PlayerDB(@NotNull MagicMOTD plugin) {

/**
* Creates a connection pool to the database.
* @throws RuntimeException if the H2 database driver cannot be found
*
* @throws RuntimeException if the H2 database driver cannot be found
* @throws IllegalStateException if the database is already ready
*/
public void readyConnections() {
Expand All @@ -59,6 +61,7 @@ public void readyConnections() {

/**
* Returns whether the database is ready to be used.
*
* @return whether the database is ready
*/
public boolean isReady() {
Expand All @@ -67,6 +70,7 @@ public boolean isReady() {

/**
* Destroys the connection pool.
*
* @throws IllegalStateException if the database is not ready
*/
public void destroyConnections() {
Expand All @@ -81,8 +85,9 @@ public void destroyConnections() {

/**
* Creates the database and its table if they don't exist.
* @throws SQLException if an error occurs while creating the database or table
* @throws RuntimeException if the connection fails
*
* @throws SQLException if an error occurs while creating the database or table
* @throws RuntimeException if the connection fails
* @throws IllegalStateException if the database is not ready
*/
public void createIfNotExists() throws SQLException {
Expand All @@ -108,12 +113,12 @@ public void createIfNotExists() throws SQLException {
/**
* Updates the player name for the given IP address.
*
* @param ip the player's IP address
* @param name the player name
* @throws SQLException if an error occurs while updating the player name
* @param ip the player's IP address
* @param name the player name
* @throws SQLException if an error occurs while updating the player name
* @throws IllegalArgumentException if the IP or name is null
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
*/
public void setNameForIP(@NotNull String ip, @NotNull String name) throws SQLException {
if (ip == null) {
Expand All @@ -134,16 +139,18 @@ public void setNameForIP(@NotNull String ip, @NotNull String name) throws SQLExc
throw new RuntimeException("Connection is null!");
}

PreparedStatement statement = conn.prepareStatement("INSERT INTO PLAYERS (ip, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?");
statement.setString(1, ip);
statement.setString(2, name);
statement.setString(3, name);
statement.execute();
try (PreparedStatement statement = conn.prepareStatement("INSERT INTO PLAYERS (ip, name) VALUES (?, ?) ON DUPLICATE KEY UPDATE name = ?")) {
statement.setString(1, ip);
statement.setString(2, name);
statement.setString(3, name);
statement.execute();
}
}
}

/**
* Returns the player name for the given IP address, or null if the IP address is not in the database.
*
* @param ip the player's IP address
* @return the player name, or null if the IP address is not in the database
*/
Expand All @@ -161,12 +168,14 @@ public void setNameForIP(@NotNull String ip, @NotNull String name) throws SQLExc
throw new RuntimeException("Connection is null!");
}

PreparedStatement statement = conn.prepareStatement("SELECT name FROM PLAYERS WHERE ip = ?");
statement.setString(1, ip);
ResultSet result = statement.executeQuery();
try (PreparedStatement statement = conn.prepareStatement("SELECT name FROM PLAYERS WHERE ip = ?")) {
statement.setString(1, ip);

if (result.next()) {
return result.getString("name");
try (ResultSet result = statement.executeQuery()) {
if (result.next()) {
return result.getString("name");
}
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
Expand All @@ -177,11 +186,12 @@ public void setNameForIP(@NotNull String ip, @NotNull String name) throws SQLExc

/**
* Returns a list of IPs known for the given player name.
*
* @param name the player name
* @return a list of IPs for the given player name
* @throws IllegalArgumentException if the name is null
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
*/
public @NotNull List<String> getIPsForName(@NotNull String name) {
if (name == null) {
Expand All @@ -192,33 +202,38 @@ public void setNameForIP(@NotNull String ip, @NotNull String name) throws SQLExc
throw new IllegalStateException("Database is not ready!");
}

// TODO: the try-with chaining kinda sucks, perhaps we could encapsulate the process in a method?
try (Connection conn = this.pool.getConnection()) {
if (conn == null) {
throw new RuntimeException("Connection is null!");
}

PreparedStatement statement = conn.prepareStatement("SELECT ip FROM PLAYERS WHERE name = ?");
statement.setString(1, name);
ResultSet result = statement.executeQuery();
try (PreparedStatement statement = conn.prepareStatement("SELECT ip FROM PLAYERS WHERE name = ?")) {
statement.setString(1, name);

// convert result set to list
List<String> ips = new ArrayList<>();
while (result.next()) {
ips.add(result.getString("ip"));
}
try (ResultSet result = statement.executeQuery()) {

// convert result set to list
List<String> ips = new ArrayList<>();
while (result.next()) {
ips.add(result.getString("ip"));
}

return ips;
return ips;
}
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

/**
* Erases the given IP address from the database.
*
* @param ip the IP address to erase
* @throws IllegalArgumentException if the IP is null
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
*/
public void eraseIP(@NotNull String ip) {
if (ip == null) {
Expand All @@ -234,20 +249,22 @@ public void eraseIP(@NotNull String ip) {
throw new RuntimeException("Connection is null!");
}

PreparedStatement statement = conn.prepareStatement("DELETE FROM PLAYERS WHERE ip = ?");
statement.setString(1, ip);
statement.execute();
try (PreparedStatement statement = conn.prepareStatement("DELETE FROM PLAYERS WHERE ip = ?")) {
statement.setString(1, ip);
statement.execute();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
}

/**
* Erases all records (IP addresses) for the given player name.
*
* @param name the player name
* @throws IllegalArgumentException if the name is null
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
* @throws IllegalStateException if the database is not ready
* @throws RuntimeException if the connection fails
*/
public void eraseName(@NotNull String name) {
if (name == null) {
Expand All @@ -263,9 +280,10 @@ public void eraseName(@NotNull String name) {
throw new RuntimeException("Connection is null!");
}

PreparedStatement statement = conn.prepareStatement("DELETE FROM PLAYERS WHERE name = ?");
statement.setString(1, name);
statement.execute();
try (PreparedStatement statement = conn.prepareStatement("DELETE FROM PLAYERS WHERE name = ?")) {
statement.setString(1, name);
statement.execute();
}
} catch (SQLException e) {
throw new RuntimeException(e);
}
Expand Down

0 comments on commit 4dedc10

Please sign in to comment.