From 1e93ad0b1e5ba61a20aaf098792decb792114957 Mon Sep 17 00:00:00 2001 From: AlexTheCoder Date: Thu, 11 May 2017 21:18:05 -0400 Subject: [PATCH] Proper resource management for leaderboard statements --- .../leaderboard/LeaderboardRepository.java | 76 +++++++++---------- 1 file changed, 34 insertions(+), 42 deletions(-) diff --git a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java index 68f1133e0..d52b487d7 100644 --- a/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java +++ b/Plugins/Mineplex.Core/src/mineplex/core/leaderboard/LeaderboardRepository.java @@ -49,13 +49,8 @@ public class LeaderboardRepository extends RepositoryBase { UtilServer.runAsync(() -> { - try (Connection c = getConnection()) + try (Connection c = getConnection(); PreparedStatement s = c.prepareStatement(UPDATE_STAT); PreparedStatement u = c.prepareStatement(UPDATE_STAT_ALL); PreparedStatement v = c.prepareStatement(INSERT_STAT); PreparedStatement w = c.prepareStatement(INSERT_STAT_ALL)) { - final boolean auto = c.getAutoCommit(); - - c.setAutoCommit(true); - PreparedStatement s = c.prepareStatement(UPDATE_STAT); - PreparedStatement u = c.prepareStatement(UPDATE_STAT_ALL); for (Integer accountId : stats.keySet()) { for (Integer statId : stats.get(accountId).keySet()) @@ -72,9 +67,6 @@ public class LeaderboardRepository extends RepositoryBase } int[] rowsAffected1 = s.executeBatch(); int[] rowsAffected2 = u.executeBatch(); - c.setAutoCommit(false); - s = c.prepareStatement(INSERT_STAT); - u = c.prepareStatement(INSERT_STAT_ALL); int i = 0; for (Integer accountId : stats.keySet()) { @@ -82,25 +74,23 @@ public class LeaderboardRepository extends RepositoryBase { if (rowsAffected1[i] < 1) { - s.setInt(1, accountId); - s.setInt(2, statId); - s.setLong(3, stats.get(accountId).get(statId)); - s.addBatch(); + v.setInt(1, accountId); + v.setInt(2, statId); + v.setLong(3, stats.get(accountId).get(statId)); + v.addBatch(); } if (rowsAffected2[i] < 1) { - u.setInt(1, accountId); - u.setInt(2, statId); - u.setLong(3, stats.get(accountId).get(statId)); - u.addBatch(); + w.setInt(1, accountId); + w.setInt(2, statId); + w.setLong(3, stats.get(accountId).get(statId)); + w.addBatch(); } i++; } } - s.executeBatch(); - u.executeBatch(); - - c.setAutoCommit(auto); + v.executeBatch(); + w.executeBatch(); } catch (SQLException e) { @@ -111,38 +101,42 @@ public class LeaderboardRepository extends RepositoryBase public void insertStats(int accountId, Map stats) { - try (Connection c = getConnection()) + try (Connection c = getConnection(); PreparedStatement s = c.prepareStatement(UPDATE_STAT); PreparedStatement u = c.prepareStatement(UPDATE_STAT_ALL); PreparedStatement v = c.prepareStatement(INSERT_STAT); PreparedStatement w = c.prepareStatement(INSERT_STAT_ALL)) { - final boolean auto = c.getAutoCommit(); - - c.setAutoCommit(true); - PreparedStatement s = c.prepareStatement(UPDATE_STAT); for (Integer statId : stats.keySet()) { s.setLong(1, stats.get(statId)); s.setInt(2, accountId); s.setInt(3, statId); s.addBatch(); + u.setLong(1, stats.get(statId)); + u.setInt(2, accountId); + u.setInt(3, statId); + u.addBatch(); } - int[] rowsAffected = s.executeBatch(); - c.setAutoCommit(false); - s = c.prepareStatement(INSERT_STAT); + int[] rowsAffected1 = s.executeBatch(); + int[] rowsAffected2 = u.executeBatch(); int i = 0; for (Integer statId : stats.keySet()) { - if (rowsAffected[i] < 1) + if (rowsAffected1[i] < 1) { - s.setInt(1, accountId); - s.setInt(2, statId); - s.setLong(3, stats.get(statId)); - s.addBatch(); + v.setInt(1, accountId); + v.setInt(2, statId); + v.setLong(3, stats.get(statId)); + v.addBatch(); + } + if (rowsAffected2[i] < 1) + { + w.setInt(1, accountId); + w.setInt(2, statId); + w.setLong(3, stats.get(statId)); + w.addBatch(); } i++; } - s.executeBatch(); - - c.setAutoCommit(auto); - + v.executeBatch(); + w.executeBatch(); } catch (SQLException e) { @@ -153,9 +147,8 @@ public class LeaderboardRepository extends RepositoryBase public void loadLeaderboard(Leaderboard board, Consumer> leaderboard) { Map names = new LinkedHashMap<>(); - try (Connection c = getConnection()) + try (Connection c = getConnection(); Statement s = c.createStatement()) { - Statement s = c.createStatement(); s.execute(board.getType().getStatement(board.getStatIds(), board.getStart(), board.getSize())); for (int i = 0; i < board.getStatIds().length; i++) { @@ -200,9 +193,8 @@ public class LeaderboardRepository extends RepositoryBase if (queryBuilder.length() > 0) { - try (Connection c = getConnection()) + try (Connection c = getConnection(); Statement s = c.createStatement()) { - Statement s = c.createStatement(); s.execute(queryBuilder.toString()); int index = 0; mainBoardLoop: for (Leaderboard board : boards)