refactor: use try-with-resources for ranking db operations
This commit is contained in:
@@ -21,14 +21,15 @@
|
|||||||
*/
|
*/
|
||||||
package net.server.task;
|
package net.server.task;
|
||||||
|
|
||||||
|
import client.MapleJob;
|
||||||
|
import config.YamlConfig;
|
||||||
|
import net.server.Server;
|
||||||
|
import tools.DatabaseConnection;
|
||||||
|
|
||||||
import java.sql.Connection;
|
import java.sql.Connection;
|
||||||
import java.sql.PreparedStatement;
|
import java.sql.PreparedStatement;
|
||||||
import java.sql.ResultSet;
|
import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import client.MapleJob;
|
|
||||||
import config.YamlConfig;
|
|
||||||
import tools.DatabaseConnection;
|
|
||||||
import net.server.Server;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Matze
|
* @author Matze
|
||||||
@@ -36,13 +37,14 @@ import net.server.Server;
|
|||||||
* @author Ronan
|
* @author Ronan
|
||||||
*/
|
*/
|
||||||
public class RankingLoginTask implements Runnable {
|
public class RankingLoginTask implements Runnable {
|
||||||
private Connection con;
|
|
||||||
private long lastUpdate = System.currentTimeMillis();
|
private long lastUpdate = System.currentTimeMillis();
|
||||||
|
|
||||||
private void resetMoveRank(boolean job) throws SQLException {
|
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");
|
||||||
PreparedStatement reset = con.prepareStatement(query);
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
reset.executeUpdate();
|
PreparedStatement reset = con.prepareStatement(query);
|
||||||
|
reset.executeUpdate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateRanking(int job, int world) throws SQLException {
|
private void updateRanking(int job, int world) throws SQLException {
|
||||||
@@ -51,66 +53,63 @@ public class RankingLoginTask implements Runnable {
|
|||||||
sqlCharSelect += "AND c.job DIV 100 = ? ";
|
sqlCharSelect += "AND c.job DIV 100 = ? ";
|
||||||
}
|
}
|
||||||
sqlCharSelect += "ORDER BY c.level DESC , c.exp DESC , c.lastExpGainTime ASC, c.fame DESC , c.meso DESC";
|
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();
|
||||||
charSelect.setInt(1, world);
|
PreparedStatement charSelect = con.prepareStatement(sqlCharSelect)) {
|
||||||
if (job != -1) {
|
charSelect.setInt(1, world);
|
||||||
charSelect.setInt(2, job);
|
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 = ?");
|
|
||||||
int rank = 0;
|
try (ResultSet rs = charSelect.executeQuery();
|
||||||
|
PreparedStatement ps = con.prepareStatement("UPDATE characters SET " + (job != -1 ? "jobRank = ?, jobRankMove = ? " : "rank = ?, rankMove = ? ") + "WHERE id = ?")) {
|
||||||
while (rs.next()) {
|
int rank = 0;
|
||||||
int rankMove = 0;
|
|
||||||
rank++;
|
while (rs.next()) {
|
||||||
if (rs.getLong("lastlogin") < lastUpdate || rs.getInt("loggedin") > 0) {
|
int rankMove = 0;
|
||||||
rankMove = rs.getInt((job != -1 ? "jobRankMove" : "rankMove"));
|
rank++;
|
||||||
|
if (rs.getLong("lastlogin") < lastUpdate || rs.getInt("loggedin") > 0) {
|
||||||
|
rankMove = rs.getInt((job != -1 ? "jobRankMove" : "rankMove"));
|
||||||
|
}
|
||||||
|
rankMove += rs.getInt((job != -1 ? "jobRank" : "rank")) - rank;
|
||||||
|
ps.setInt(1, rank);
|
||||||
|
ps.setInt(2, rankMove);
|
||||||
|
ps.setInt(3, rs.getInt("id"));
|
||||||
|
ps.executeUpdate();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
rankMove += rs.getInt((job != -1 ? "jobRank" : "rank")) - rank;
|
|
||||||
ps.setInt(1, rank);
|
|
||||||
ps.setInt(2, rankMove);
|
|
||||||
ps.setInt(3, rs.getInt("id"));
|
|
||||||
ps.executeUpdate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rs.close();
|
|
||||||
charSelect.close();
|
|
||||||
ps.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try (Connection con = DatabaseConnection.getConnection()) {
|
||||||
con = DatabaseConnection.getConnection();
|
|
||||||
con.setAutoCommit(false);
|
con.setAutoCommit(false);
|
||||||
|
|
||||||
if(YamlConfig.config.server.USE_REFRESH_RANK_MOVE == true) {
|
|
||||||
resetMoveRank(true);
|
|
||||||
resetMoveRank(false);
|
|
||||||
}
|
|
||||||
|
|
||||||
for(int j = 0; j < Server.getInstance().getWorldsSize(); j++) {
|
|
||||||
updateRanking(-1, j); //overall ranking
|
|
||||||
for (int i = 0; i <= MapleJob.getMax(); i++) {
|
|
||||||
updateRanking(i, j);
|
|
||||||
}
|
|
||||||
con.commit();
|
|
||||||
}
|
|
||||||
|
|
||||||
con.setAutoCommit(true);
|
|
||||||
lastUpdate = System.currentTimeMillis();
|
|
||||||
con.close();
|
|
||||||
} catch (SQLException ex) {
|
|
||||||
ex.printStackTrace();
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
con.rollback();
|
if (YamlConfig.config.server.USE_REFRESH_RANK_MOVE) {
|
||||||
|
resetMoveRank(true);
|
||||||
|
resetMoveRank(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int j = 0; j < Server.getInstance().getWorldsSize(); j++) {
|
||||||
|
updateRanking(-1, j); //overall ranking
|
||||||
|
for (int i = 0; i <= MapleJob.getMax(); i++) {
|
||||||
|
updateRanking(i, j);
|
||||||
|
}
|
||||||
|
con.commit();
|
||||||
|
}
|
||||||
|
|
||||||
|
con.setAutoCommit(true);
|
||||||
|
lastUpdate = System.currentTimeMillis();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
con.rollback();
|
||||||
|
throw ex;
|
||||||
|
} finally {
|
||||||
con.setAutoCommit(true);
|
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