Proper resource management for leaderboard statements

This commit is contained in:
AlexTheCoder 2017-05-11 21:18:05 -04:00
parent a76572f6d7
commit 1e93ad0b1e

View File

@ -49,13 +49,8 @@ public class LeaderboardRepository extends RepositoryBase
{ {
UtilServer.runAsync(() -> 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 accountId : stats.keySet())
{ {
for (Integer statId : stats.get(accountId).keySet()) for (Integer statId : stats.get(accountId).keySet())
@ -72,9 +67,6 @@ public class LeaderboardRepository extends RepositoryBase
} }
int[] rowsAffected1 = s.executeBatch(); int[] rowsAffected1 = s.executeBatch();
int[] rowsAffected2 = u.executeBatch(); int[] rowsAffected2 = u.executeBatch();
c.setAutoCommit(false);
s = c.prepareStatement(INSERT_STAT);
u = c.prepareStatement(INSERT_STAT_ALL);
int i = 0; int i = 0;
for (Integer accountId : stats.keySet()) for (Integer accountId : stats.keySet())
{ {
@ -82,25 +74,23 @@ public class LeaderboardRepository extends RepositoryBase
{ {
if (rowsAffected1[i] < 1) if (rowsAffected1[i] < 1)
{ {
s.setInt(1, accountId); v.setInt(1, accountId);
s.setInt(2, statId); v.setInt(2, statId);
s.setLong(3, stats.get(accountId).get(statId)); v.setLong(3, stats.get(accountId).get(statId));
s.addBatch(); v.addBatch();
} }
if (rowsAffected2[i] < 1) if (rowsAffected2[i] < 1)
{ {
u.setInt(1, accountId); w.setInt(1, accountId);
u.setInt(2, statId); w.setInt(2, statId);
u.setLong(3, stats.get(accountId).get(statId)); w.setLong(3, stats.get(accountId).get(statId));
u.addBatch(); w.addBatch();
} }
i++; i++;
} }
} }
s.executeBatch(); v.executeBatch();
u.executeBatch(); w.executeBatch();
c.setAutoCommit(auto);
} }
catch (SQLException e) catch (SQLException e)
{ {
@ -111,38 +101,42 @@ public class LeaderboardRepository extends RepositoryBase
public void insertStats(int accountId, Map<Integer, Long> stats) public void insertStats(int accountId, Map<Integer, Long> 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()) for (Integer statId : stats.keySet())
{ {
s.setLong(1, stats.get(statId)); s.setLong(1, stats.get(statId));
s.setInt(2, accountId); s.setInt(2, accountId);
s.setInt(3, statId); s.setInt(3, statId);
s.addBatch(); s.addBatch();
u.setLong(1, stats.get(statId));
u.setInt(2, accountId);
u.setInt(3, statId);
u.addBatch();
} }
int[] rowsAffected = s.executeBatch(); int[] rowsAffected1 = s.executeBatch();
c.setAutoCommit(false); int[] rowsAffected2 = u.executeBatch();
s = c.prepareStatement(INSERT_STAT);
int i = 0; int i = 0;
for (Integer statId : stats.keySet()) for (Integer statId : stats.keySet())
{ {
if (rowsAffected[i] < 1) if (rowsAffected1[i] < 1)
{ {
s.setInt(1, accountId); v.setInt(1, accountId);
s.setInt(2, statId); v.setInt(2, statId);
s.setLong(3, stats.get(statId)); v.setLong(3, stats.get(statId));
s.addBatch(); v.addBatch();
}
if (rowsAffected2[i] < 1)
{
w.setInt(1, accountId);
w.setInt(2, statId);
w.setLong(3, stats.get(statId));
w.addBatch();
} }
i++; i++;
} }
s.executeBatch(); v.executeBatch();
w.executeBatch();
c.setAutoCommit(auto);
} }
catch (SQLException e) catch (SQLException e)
{ {
@ -153,9 +147,8 @@ public class LeaderboardRepository extends RepositoryBase
public void loadLeaderboard(Leaderboard board, Consumer<Map<String, Integer>> leaderboard) public void loadLeaderboard(Leaderboard board, Consumer<Map<String, Integer>> leaderboard)
{ {
Map<String, Integer> names = new LinkedHashMap<>(); Map<String, Integer> 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())); s.execute(board.getType().getStatement(board.getStatIds(), board.getStart(), board.getSize()));
for (int i = 0; i < board.getStatIds().length; i++) for (int i = 0; i < board.getStatIds().length; i++)
{ {
@ -200,9 +193,8 @@ public class LeaderboardRepository extends RepositoryBase
if (queryBuilder.length() > 0) if (queryBuilder.length() > 0)
{ {
try (Connection c = getConnection()) try (Connection c = getConnection(); Statement s = c.createStatement())
{ {
Statement s = c.createStatement();
s.execute(queryBuilder.toString()); s.execute(queryBuilder.toString());
int index = 0; int index = 0;
mainBoardLoop: for (Leaderboard board : boards) mainBoardLoop: for (Leaderboard board : boards)