refactor: use try-with-resources for ranking db operations
This commit is contained in:
@@ -21,14 +21,15 @@
|
||||
*/
|
||||
package net.server.task;
|
||||
|
||||
import client.MapleJob;
|
||||
import config.YamlConfig;
|
||||
import net.server.Server;
|
||||
import tools.DatabaseConnection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import client.MapleJob;
|
||||
import config.YamlConfig;
|
||||
import tools.DatabaseConnection;
|
||||
import net.server.Server;
|
||||
|
||||
/**
|
||||
* @author Matze
|
||||
@@ -36,14 +37,15 @@ import net.server.Server;
|
||||
* @author Ronan
|
||||
*/
|
||||
public class RankingLoginTask implements Runnable {
|
||||
private Connection con;
|
||||
private long lastUpdate = System.currentTimeMillis();
|
||||
|
||||
private void resetMoveRank(boolean job) throws SQLException {
|
||||
String query = "UPDATE characters SET " + (job == true ? "jobRankMove = 0" : "rankMove = 0");
|
||||
String query = "UPDATE characters SET " + (job ? "jobRankMove = 0" : "rankMove = 0");
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
PreparedStatement reset = con.prepareStatement(query);
|
||||
reset.executeUpdate();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateRanking(int job, int world) throws SQLException {
|
||||
String sqlCharSelect = "SELECT c.id, " + (job != -1 ? "c.jobRank, c.jobRankMove" : "c.rank, c.rankMove") + ", a.lastlogin AS lastlogin, a.loggedin FROM characters AS c LEFT JOIN accounts AS a ON c.accountid = a.id WHERE c.gm < 2 AND c.world = ? ";
|
||||
@@ -52,13 +54,15 @@ public class RankingLoginTask implements Runnable {
|
||||
}
|
||||
sqlCharSelect += "ORDER BY c.level DESC , c.exp DESC , c.lastExpGainTime ASC, c.fame DESC , c.meso DESC";
|
||||
|
||||
PreparedStatement charSelect = con.prepareStatement(sqlCharSelect);
|
||||
try (Connection con = DatabaseConnection.getConnection();
|
||||
PreparedStatement charSelect = con.prepareStatement(sqlCharSelect)) {
|
||||
charSelect.setInt(1, world);
|
||||
if (job != -1) {
|
||||
charSelect.setInt(2, job);
|
||||
}
|
||||
ResultSet rs = charSelect.executeQuery();
|
||||
PreparedStatement ps = con.prepareStatement("UPDATE characters SET " + (job != -1 ? "jobRank = ?, jobRankMove = ? " : "rank = ?, rankMove = ? ") + "WHERE id = ?");
|
||||
|
||||
try (ResultSet rs = charSelect.executeQuery();
|
||||
PreparedStatement ps = con.prepareStatement("UPDATE characters SET " + (job != -1 ? "jobRank = ?, jobRankMove = ? " : "rank = ?, rankMove = ? ") + "WHERE id = ?")) {
|
||||
int rank = 0;
|
||||
|
||||
while (rs.next()) {
|
||||
@@ -73,19 +77,17 @@ public class RankingLoginTask implements Runnable {
|
||||
ps.setInt(3, rs.getInt("id"));
|
||||
ps.executeUpdate();
|
||||
}
|
||||
|
||||
rs.close();
|
||||
charSelect.close();
|
||||
ps.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
con = DatabaseConnection.getConnection();
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
con.setAutoCommit(false);
|
||||
|
||||
if(YamlConfig.config.server.USE_REFRESH_RANK_MOVE == true) {
|
||||
try {
|
||||
if (YamlConfig.config.server.USE_REFRESH_RANK_MOVE) {
|
||||
resetMoveRank(true);
|
||||
resetMoveRank(false);
|
||||
}
|
||||
@@ -100,17 +102,14 @@ public class RankingLoginTask implements Runnable {
|
||||
|
||||
con.setAutoCommit(true);
|
||||
lastUpdate = System.currentTimeMillis();
|
||||
con.close();
|
||||
} catch (SQLException ex) {
|
||||
ex.printStackTrace();
|
||||
|
||||
try {
|
||||
con.rollback();
|
||||
throw ex;
|
||||
} finally {
|
||||
con.setAutoCommit(true);
|
||||
if(!con.isClosed()) con.close();
|
||||
} catch (SQLException ex2) {
|
||||
ex2.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user