Proper resource management for leaderboard statements

This commit is contained in:
AlexTheCoder 2017-05-11 21:18:05 -04:00
parent a76572f6d7
commit 1e93ad0b1e
1 changed files with 34 additions and 42 deletions

View File

@ -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<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())
{
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<Map<String, Integer>> leaderboard)
{
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()));
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)