Prevent user purges and UUID updates from conflicting in mass database operations.

This commit is contained in:
t00thpick1 2014-08-04 11:42:02 -04:00
parent 432ff95a98
commit f4c53aaf8a

View File

@ -13,6 +13,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.config.Config; import com.gmail.nossr50.config.Config;
@ -40,6 +43,8 @@ public final class SQLDatabaseManager implements DatabaseManager {
private ConnectionPool connectionPool; private ConnectionPool connectionPool;
private ReentrantLock massUpdateLock = new ReentrantLock();
protected SQLDatabaseManager() { protected SQLDatabaseManager() {
String connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName(); String connectionString = "jdbc:mysql://" + Config.getInstance().getMySQLServerName() + ":" + Config.getInstance().getMySQLServerPort() + "/" + Config.getInstance().getMySQLDatabaseName();
@ -76,6 +81,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
public void purgePowerlessUsers() { public void purgePowerlessUsers() {
massUpdateLock.lock();
mcMMO.p.getLogger().info("Purging powerless users..."); mcMMO.p.getLogger().info("Purging powerless users...");
Connection connection = null; Connection connection = null;
@ -113,12 +119,14 @@ public final class SQLDatabaseManager implements DatabaseManager {
// Ignore // Ignore
} }
} }
massUpdateLock.unlock();
} }
mcMMO.p.getLogger().info("Purged " + purged + " users from the database."); mcMMO.p.getLogger().info("Purged " + purged + " users from the database.");
} }
public void purgeOldUsers() { public void purgeOldUsers() {
massUpdateLock.lock();
mcMMO.p.getLogger().info("Purging old users..."); mcMMO.p.getLogger().info("Purging old users...");
Connection connection = null; Connection connection = null;
@ -156,6 +164,7 @@ public final class SQLDatabaseManager implements DatabaseManager {
// Ignore // Ignore
} }
} }
massUpdateLock.unlock();
} }
mcMMO.p.getLogger().info("Purged " + purged + " users from the database."); mcMMO.p.getLogger().info("Purged " + purged + " users from the database.");
@ -1360,7 +1369,6 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
private void checkUpgradeAddUUIDs(final Statement statement) { private void checkUpgradeAddUUIDs(final Statement statement) {
List<String> names = new ArrayList<String>();
ResultSet resultSet = null; ResultSet resultSet = null;
try { try {
@ -1396,29 +1404,57 @@ public final class SQLDatabaseManager implements DatabaseManager {
} }
} }
try { new GetUUIDUpdatesRequired().runTaskAsynchronously(mcMMO.p);
resultSet = statement.executeQuery("SELECT `user` FROM `" + tablePrefix + "users` WHERE `uuid` IS NULL"); }
while (resultSet.next()) { private class GetUUIDUpdatesRequired extends BukkitRunnable {
names.add(resultSet.getString("user")); public void run() {
} massUpdateLock.lock();
} List<String> names = new ArrayList<String>();
catch (SQLException ex) { Connection connection = null;
printErrors(ex); Statement statement = null;
} ResultSet resultSet = null;
finally { try {
if (resultSet != null) {
try { try {
resultSet.close(); connection = connectionPool.getConnection();
} statement = connection.createStatement();
catch (SQLException e) { resultSet = statement.executeQuery("SELECT `user` FROM `" + tablePrefix + "users` WHERE `uuid` IS NULL");
// Ignore
}
}
}
if (!names.isEmpty()) { while (resultSet.next()) {
new UUIDUpdateAsyncTask(mcMMO.p, names).runTaskAsynchronously(mcMMO.p); names.add(resultSet.getString("user"));
}
} catch (SQLException ex) {
printErrors(ex);
} finally {
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException e) {
// Ignore
}
}
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// Ignore
}
}
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// Ignore
}
}
}
if (!names.isEmpty()) {
new UUIDUpdateAsyncTask(mcMMO.p, names).run();;
}
} finally {
massUpdateLock.unlock();
}
} }
} }