SQLDatabaseManager optimizations, async profile loading -t00thpick1, zreed

This commit changes our shared connection into a connection pool utility to prevent
thread locks from multiple actions attempting to access the database at the same time.
In additon,  profile loading has been moved off the main thread at login time, to
allieviate the performance issues caused by it.

Fixes #2138, Fixes #2119, Fixes #1982, Fixes #1953
This commit is contained in:
t00thpick1
2014-08-01 13:35:36 -04:00
committed by TfT_02
parent c10525ada9
commit 857e12b96e
20 changed files with 1227 additions and 1161 deletions

View File

@ -1,39 +0,0 @@
package com.gmail.nossr50.runnables.database;
import java.lang.ref.WeakReference;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.database.SQLDatabaseManager;
/**
* This task is in charge of sending a MySQL ping over the MySQL connection
* every hour to prevent the connection from timing out and losing players'
* data when they join.
* <p/>
* A WeakReference is used to keep the database instance, because
* {@link com.gmail.nossr50.commands.database.ConvertDatabaseCommand database
* conversion} may create a SQLDatabaseManager that will be thrown out. If a
* normal reference was used, the conversion would cause a combined data and
* resource leak through this task.
*/
public class SQLDatabaseKeepaliveTask extends BukkitRunnable {
WeakReference<SQLDatabaseManager> databaseInstance;
public SQLDatabaseKeepaliveTask(SQLDatabaseManager dbman) {
databaseInstance = new WeakReference<SQLDatabaseManager>(dbman);
}
public void run() {
SQLDatabaseManager dbman = databaseInstance.get();
if (dbman != null) {
dbman.checkConnected();
}
else {
// This happens when the database was started for a conversion,
// or discarded by its creator for any other reason. If this code
// was not present, we would leak the connection resources.
this.cancel();
}
}
}

View File

@ -1,22 +0,0 @@
package com.gmail.nossr50.runnables.database;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.database.SQLDatabaseManager;
import com.gmail.nossr50.util.player.UserManager;
public class SQLReconnectTask extends BukkitRunnable {
@Override
public void run() {
if (((SQLDatabaseManager) mcMMO.getDatabaseManager()).checkConnected()) {
UserManager.saveAll(); // Save all profiles
UserManager.clearAll(); // Clear the profiles
for (Player player : mcMMO.p.getServer().getOnlinePlayers()) {
UserManager.addUser(player); // Add in new profiles, forcing them to 'load' again from MySQL
}
}
}
}

View File

@ -1,51 +0,0 @@
package com.gmail.nossr50.runnables.database;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.UUID;
import org.bukkit.scheduler.BukkitRunnable;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.uuid.UUIDFetcher;
public class UUIDFetcherRunnable extends BukkitRunnable {
private List<String> names;
public UUIDFetcherRunnable(List<String> names) {
this.names = names;
}
public UUIDFetcherRunnable(String name) {
this.names = new ArrayList<String>();
this.names.add(name);
}
@Override
public void run() {
try {
Map<String, UUID> returns = new UUIDFetcher(this.names).call();
new CacheReturnedNames(returns).runTask(mcMMO.p);
}
catch (Exception e) {
e.printStackTrace();
}
}
private class CacheReturnedNames extends BukkitRunnable {
private Map<String, UUID> returns;
public CacheReturnedNames(Map<String, UUID> returns) {
this.returns = returns;
}
@Override
public void run() {
for (Entry<String, UUID> entry : this.returns.entrySet()) {
mcMMO.getDatabaseManager().saveUserUUID(entry.getKey(), entry.getValue());
}
}
}
}