Skip to content

Commit

Permalink
Merge pull request #73 from AlpsBTE/72-closing-connections
Browse files Browse the repository at this point in the history
Make database connection closures compatible for MySQL
  • Loading branch information
LordTuxn authored Jan 20, 2022
2 parents 68ea3f2 + 47ad915 commit 4734883
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 29 deletions.
2 changes: 2 additions & 0 deletions src/main/java/com/alpsbte/plotsystem/core/EventListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ public void onPlayerJoinEvent(PlayerJoinEvent event) {
.setValue(event.getPlayer().getName())
.executeUpdate();
}

DatabaseConnection.closeResultSet(rs);
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class DatabaseConnection {
private static String username;
private static String password;

private static int connectionClosed, connectionOpened;

public static void InitializeDatabase() throws ClassNotFoundException, SQLException {
Class.forName("org.mariadb.jdbc.Driver");

Expand Down Expand Up @@ -90,6 +92,22 @@ public static StatementBuilder createStatement(String sql) {
return new StatementBuilder(sql);
}

public static void closeResultSet(ResultSet resultSet) throws SQLException {
if(resultSet.isClosed()
&& resultSet.getStatement().isClosed()
&& resultSet.getStatement().getConnection().isClosed())
return;

resultSet.close();
resultSet.getStatement().close();
resultSet.getStatement().getConnection().close();

connectionClosed++;

if(connectionOpened > connectionClosed + 5)
Bukkit.getLogger().log(Level.SEVERE, "There are multiple database connections opened. Please report this issue.");
}

private static void createDatabase() throws SQLException {
try (Connection con = DriverManager.getConnection(URL, username, password)) {
try (Statement statement = con.createStatement()) {
Expand Down Expand Up @@ -129,8 +147,12 @@ public static int getTableID(String table) {
.replace("$table", table);
try (ResultSet rs = DatabaseConnection.createStatement(query).executeQuery()) {
if (rs.next()) {
return rs.getInt(1);
int i = rs.getInt(1);
DatabaseConnection.closeResultSet(rs);
return i;
}

DatabaseConnection.closeResultSet(rs);
return 1;
}
} catch (SQLException ex) {
Expand All @@ -153,11 +175,13 @@ public StatementBuilder setValue(Object value) {
}

public ResultSet executeQuery() throws SQLException {
try (Connection con = dataSource.getConnection()) {
try (PreparedStatement ps = Objects.requireNonNull(con).prepareStatement(sql)) {
return iterateValues(ps).executeQuery();
}
}
Connection con = dataSource.getConnection();
PreparedStatement ps = Objects.requireNonNull(con).prepareStatement(sql);
ResultSet rs = iterateValues(ps).executeQuery();

connectionOpened++;

return rs;
}

public void executeUpdate() throws SQLException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ protected void setMenuItemsAsync() {
if (rs.next()) {
this.review = new Review(rs.getInt(1));
}

DatabaseConnection.closeResultSet(rs);
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
}
Expand Down
38 changes: 33 additions & 5 deletions src/main/java/com/alpsbte/plotsystem/core/system/Builder.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,13 @@ public String getName() throws SQLException {
.setValue(getUUID().toString()).executeQuery()) {

if (rs.next()) {
return rs.getString(1);
String s = rs.getString(1);
DatabaseConnection.closeResultSet(rs);
return s;
}

DatabaseConnection.closeResultSet(rs);

return getPlayer() != null ? getPlayer().getName() : "";
}
}
Expand All @@ -74,8 +79,12 @@ public int getScore() throws SQLException {
.setValue(getUUID().toString()).executeQuery()) {

if (rs.next()) {
return rs.getInt(1);
int i = rs.getInt(1);
DatabaseConnection.closeResultSet(rs);
return i;
}

DatabaseConnection.closeResultSet(rs);
return 0;
}
}
Expand All @@ -85,8 +94,12 @@ public int getCompletedBuilds() throws SQLException {
.setValue(getUUID().toString()).executeQuery()) {

if (rs.next()) {
return rs.getInt(1);
int i = rs.getInt(1);
DatabaseConnection.closeResultSet(rs);
return i;
}

DatabaseConnection.closeResultSet(rs);
return 0;
}
}
Expand All @@ -98,10 +111,13 @@ public Slot getFreeSlot() throws SQLException {
if (rs.next()) {
for(int i = 1; i <= 3; i++) {
if(rs.getString(i) == null) {
DatabaseConnection.closeResultSet(rs);
return Slot.values()[i - 1];
}
}
}

DatabaseConnection.closeResultSet(rs);
return null;
}
}
Expand All @@ -112,7 +128,11 @@ public Plot getPlot(Slot slot) throws SQLException {

int plotID = -1;
if (rs.next()) plotID = rs.getInt(1);
return rs.wasNull() ? null : new Plot(plotID);

boolean boo = rs.wasNull();
DatabaseConnection.closeResultSet(rs);

return boo ? null : new Plot(plotID);
}
}

Expand Down Expand Up @@ -151,8 +171,12 @@ public static Builder getBuilderByName(String name) throws SQLException {
.setValue(name).executeQuery()) {

if (rs.next()) {
return new Builder(java.util.UUID.fromString(rs.getString(1)));
String s = rs.getString(1);
DatabaseConnection.closeResultSet(rs);
return new Builder(java.util.UUID.fromString(s));
}

DatabaseConnection.closeResultSet(rs);
return null;
}
}
Expand All @@ -165,6 +189,8 @@ public static List<String> getBuildersByScore(int limit) throws SQLException {
while (rs.next()) {
scoreAsFormat.add(rs.getString(1) + "," + rs.getInt(2));
}

DatabaseConnection.closeResultSet(rs);
return scoreAsFormat;
}
}
Expand All @@ -177,6 +203,8 @@ public static List<String> getBuildersByCompletedBuilds(int limit) throws SQLExc
while (rs.next()) {
scoreAsFormat.add(rs.getString(1) + "," + rs.getInt(2));
}

DatabaseConnection.closeResultSet(rs);
return scoreAsFormat;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ public CityProject(int ID) throws SQLException {
this.description = rs.getString(3);
this.visible = rs.getInt(4) == 1;
}

DatabaseConnection.closeResultSet(rs);
}
}

Expand Down Expand Up @@ -86,6 +88,8 @@ public static List<CityProject> getCityProjects(boolean onlyVisible) {
cityProjects.add(city);
}
}

DatabaseConnection.closeResultSet(rs);
return cityProjects;
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/alpsbte/plotsystem/core/system/Country.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public Country(int ID) throws SQLException {
this.name = rs.getString(2);
this.headID = rs.getString(3);
}

DatabaseConnection.closeResultSet(rs);
}
}

Expand All @@ -55,6 +57,8 @@ public static List<Country> getCountries() {
while (rs.next()) {
countries.add(new Country(rs.getInt(1)));
}

DatabaseConnection.closeResultSet(rs);
return countries;
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public Difficulty(int ID) throws SQLException {
this.multiplier = rs.getDouble(2);
this.scoreRequirement = rs.getInt(3);
}

DatabaseConnection.closeResultSet(rs);
}
}

Expand All @@ -53,6 +55,9 @@ public static List<Difficulty> getDifficulties() {
while (rs.next()) {
difficulties.add(new Difficulty(rs.getInt(1)));
}

DatabaseConnection.closeResultSet(rs);

return difficulties;
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public FTPConfiguration(int ID) throws SQLException {
this.username = rs.getString(5);
this.password = rs.getString(6);
}

DatabaseConnection.closeResultSet(rs);
}
}

Expand Down Expand Up @@ -75,6 +77,9 @@ public static List<FTPConfiguration> getFTPConfigurations() {
while (rs.next()) {
ftpConfigurations.add(new FTPConfiguration(rs.getInt(1)));
}

DatabaseConnection.closeResultSet(rs);

return ftpConfigurations;
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
Expand Down
34 changes: 29 additions & 5 deletions src/main/java/com/alpsbte/plotsystem/core/system/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,12 @@ public int getPlotID() throws SQLException {
.setValue(this.reviewID).executeQuery()) {

if (rs.next()) {
return rs.getInt(1);
int i = rs.getInt(1);
DatabaseConnection.closeResultSet(rs);
return i;
}

DatabaseConnection.closeResultSet(rs);
return 0;
}
}
Expand All @@ -87,8 +91,12 @@ public Builder getReviewer() throws SQLException {
.setValue(this.reviewID).executeQuery()) {

if (rs.next()) {
return new Builder(UUID.fromString(rs.getString(1)));
String s = rs.getString(1);
DatabaseConnection.closeResultSet(rs);
return new Builder(UUID.fromString(s));
}

DatabaseConnection.closeResultSet(rs);
return null;
}
}
Expand All @@ -99,6 +107,8 @@ public int getRating(Category category) throws SQLException {

if (rs.next()) {
String[] scoreAsString = rs.getString("rating").split(",");
DatabaseConnection.closeResultSet(rs);

switch (category) {
case ACCURACY:
return Integer.parseInt(scoreAsString[0]);
Expand All @@ -114,6 +124,8 @@ public int getRating(Category category) throws SQLException {
return 0;
}
}

DatabaseConnection.closeResultSet(rs);
return 0;
}
}
Expand All @@ -123,8 +135,12 @@ public String getFeedback() throws SQLException {
.setValue(this.reviewID).executeQuery()) {

if (rs.next()) {
return rs.getString(1);
String s = rs.getString(1);
DatabaseConnection.closeResultSet(rs);
return s;
}

DatabaseConnection.closeResultSet(rs);
return null;
}
}
Expand All @@ -134,8 +150,12 @@ public Date getReviewDate() throws SQLException {
.setValue(this.reviewID).executeQuery()) {

if (rs.next()) {
return rs.getDate(1);
Date d = rs.getDate(1);
DatabaseConnection.closeResultSet(rs);
return d;
}

DatabaseConnection.closeResultSet(rs);
return null;
}
}
Expand Down Expand Up @@ -186,8 +206,12 @@ public boolean isFeedbackSent() throws SQLException {
.setValue(this.reviewID).executeQuery()) {

if (rs.next()) {
return rs.getInt(1) != 0;
int i = rs.getInt(1);
DatabaseConnection.closeResultSet(rs);
return i != 0;
}

DatabaseConnection.closeResultSet(rs);
return false;
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/alpsbte/plotsystem/core/system/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public Server(int ID) throws SQLException {

this.name = rs.getString(2);
}

DatabaseConnection.closeResultSet(rs);
}
}

Expand All @@ -52,6 +54,9 @@ public static List<Server> getServers() {
while (rs.next()) {
servers.add(new Server(rs.getInt(1)));
}

DatabaseConnection.closeResultSet(rs);

return servers;
} catch (SQLException ex) {
Bukkit.getLogger().log(Level.SEVERE, "A SQL error occurred!", ex);
Expand Down
Loading

0 comments on commit 4734883

Please sign in to comment.