Moved our SQL Reconnect task into its own class.

This commit is contained in:
GJ 2012-04-25 21:21:35 -04:00
parent 36a5039c12
commit 747d8a6031
4 changed files with 46 additions and 32 deletions

View File

@ -9,18 +9,16 @@ import java.util.HashMap;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Properties; import java.util.Properties;
import org.bukkit.entity.Player;
import com.gmail.nossr50.config.LoadProperties; import com.gmail.nossr50.config.LoadProperties;
import com.gmail.nossr50.datatypes.DatabaseUpdate; import com.gmail.nossr50.datatypes.DatabaseUpdate;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.runnables.SQLReconnect;
public class Database { public class Database {
private String connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass; private static String connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass;
private Connection conn = null; private static Connection conn = null;
private mcMMO plugin = null; private static mcMMO plugin = null;
private long reconnectTimestamp = 0; private static long reconnectTimestamp = 0;
public Database(mcMMO instance) { public Database(mcMMO instance) {
plugin = instance; plugin = instance;
@ -43,7 +41,7 @@ public class Database {
/** /**
* Attempt to connect to the mySQL database. * Attempt to connect to the mySQL database.
*/ */
public void connect() { public static void connect() {
try { try {
System.out.println("[mcMMO] Attempting connection to MySQL..."); System.out.println("[mcMMO] Attempting connection to MySQL...");
Properties conProperties = new Properties(); Properties conProperties = new Properties();
@ -229,7 +227,7 @@ public class Database {
* *
* @return the boolean value for whether or not we are connected * @return the boolean value for whether or not we are connected
*/ */
public boolean isConnected() { public static boolean isConnected() {
if(conn == null) if(conn == null)
return false; return false;
@ -244,28 +242,11 @@ public class Database {
* Schedules a Sync Delayed Task with the Bukkit Scheduler to attempt reconnection after a minute has elapsed * Schedules a Sync Delayed Task with the Bukkit Scheduler to attempt reconnection after a minute has elapsed
* This will check for a connection being present or not to prevent unneeded reconnection attempts * This will check for a connection being present or not to prevent unneeded reconnection attempts
*/ */
public void attemptReconnect() { public static void attemptReconnect() {
if(reconnectTimestamp+60000 < System.currentTimeMillis()) //Only reconnect if another attempt hasn't been made recently if(reconnectTimestamp + 60000 < System.currentTimeMillis()) {
{ System.out.println("[mcMMO] Connection to MySQL was lost! Attempting to reconnect in 60 seconds..."); //Only reconnect if another attempt hasn't been made recently
System.out.println("[mcMMO] Connection to MySQL was lost! Attempting to reconnect in 60 seconds...");
reconnectTimestamp = System.currentTimeMillis(); reconnectTimestamp = System.currentTimeMillis();
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new SQLReconnect(plugin), 1200);
new Runnable() {
public void run() {
if (!isConnected()) {
connect();
if(isConnected()) {
for(PlayerProfile x : Users.players.values()) {
x.save(); //Save all profiles
}
Users.players.clear(); //Clear the profiles
for(Player x : plugin.getServer().getOnlinePlayers()) {
Users.addUser(x); //Add in new profiles, forcing them to 'load' again from MySQL
}
}
}
}
}, 20*60);
} }
} }

View File

@ -1,6 +1,5 @@
package com.gmail.nossr50; package com.gmail.nossr50;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
public class ItemChecks { public class ItemChecks {
@ -396,3 +395,4 @@ public class ItemChecks {
} }
} }
} }

View File

@ -21,7 +21,7 @@ public class mcLocale {
* Gets the appropriate string from the Locale files. * Gets the appropriate string from the Locale files.
* *
* @param key The key to look up the string with * @param key The key to look up the string with
* @param messageArguments Any arguements to be added to the string * @param messageArguments Any arguments to be added to the string
* @return The properly formatted locale string * @return The properly formatted locale string
*/ */
public static String getString(String key, Object[] messageArguments) { public static String getString(String key, Object[] messageArguments) {

View File

@ -0,0 +1,33 @@
package com.gmail.nossr50.runnables;
import org.bukkit.entity.Player;
import com.gmail.nossr50.Database;
import com.gmail.nossr50.Users;
import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.PlayerProfile;
public class SQLReconnect implements Runnable {
private final mcMMO plugin;
public SQLReconnect(mcMMO plugin) {
this.plugin = plugin;
}
@Override
public void run() {
if (Database.isConnected()) {
Database.connect();
if (Database.isConnected()) {
for (PlayerProfile x : Users.players.values()) {
x.save(); //Save all profiles
}
Users.players.clear(); //Clear the profiles
for (Player x : plugin.getServer().getOnlinePlayers()) {
Users.addUser(x); //Add in new profiles, forcing them to 'load' again from MySQL
}
}
}
}
}