From 747d8a6031cee37a6565440b310b0be32feae17d Mon Sep 17 00:00:00 2001 From: GJ Date: Wed, 25 Apr 2012 21:21:35 -0400 Subject: [PATCH] Moved our SQL Reconnect task into its own class. --- src/main/java/com/gmail/nossr50/Database.java | 41 +++++-------------- .../java/com/gmail/nossr50/ItemChecks.java | 2 +- .../com/gmail/nossr50/locale/mcLocale.java | 2 +- .../gmail/nossr50/runnables/SQLReconnect.java | 33 +++++++++++++++ 4 files changed, 46 insertions(+), 32 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java diff --git a/src/main/java/com/gmail/nossr50/Database.java b/src/main/java/com/gmail/nossr50/Database.java index 3f1aab191..08c33c2ad 100644 --- a/src/main/java/com/gmail/nossr50/Database.java +++ b/src/main/java/com/gmail/nossr50/Database.java @@ -9,18 +9,16 @@ import java.util.HashMap; import java.util.ArrayList; import java.util.Properties; -import org.bukkit.entity.Player; - import com.gmail.nossr50.config.LoadProperties; import com.gmail.nossr50.datatypes.DatabaseUpdate; -import com.gmail.nossr50.datatypes.PlayerProfile; +import com.gmail.nossr50.runnables.SQLReconnect; public class Database { - private String connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass; - private Connection conn = null; - private mcMMO plugin = null; - private long reconnectTimestamp = 0; + private static String connectionString = "jdbc:mysql://" + LoadProperties.MySQLserverName + ":" + LoadProperties.MySQLport + "/" + LoadProperties.MySQLdbName + "?user=" + LoadProperties.MySQLuserName + "&password=" + LoadProperties.MySQLdbPass; + private static Connection conn = null; + private static mcMMO plugin = null; + private static long reconnectTimestamp = 0; public Database(mcMMO instance) { plugin = instance; @@ -43,7 +41,7 @@ public class Database { /** * Attempt to connect to the mySQL database. */ - public void connect() { + public static void connect() { try { System.out.println("[mcMMO] Attempting connection to MySQL..."); Properties conProperties = new Properties(); @@ -229,7 +227,7 @@ public class Database { * * @return the boolean value for whether or not we are connected */ - public boolean isConnected() { + public static boolean isConnected() { if(conn == null) 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 * This will check for a connection being present or not to prevent unneeded reconnection attempts */ - public void attemptReconnect() { - if(reconnectTimestamp+60000 < System.currentTimeMillis()) //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..."); + public static void attemptReconnect() { + 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 reconnectTimestamp = System.currentTimeMillis(); - plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, - 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); + plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new SQLReconnect(plugin), 1200); } } diff --git a/src/main/java/com/gmail/nossr50/ItemChecks.java b/src/main/java/com/gmail/nossr50/ItemChecks.java index 3315a3777..4cddca808 100644 --- a/src/main/java/com/gmail/nossr50/ItemChecks.java +++ b/src/main/java/com/gmail/nossr50/ItemChecks.java @@ -1,6 +1,5 @@ package com.gmail.nossr50; -import org.bukkit.Material; import org.bukkit.inventory.ItemStack; public class ItemChecks { @@ -396,3 +395,4 @@ public class ItemChecks { } } } + \ No newline at end of file diff --git a/src/main/java/com/gmail/nossr50/locale/mcLocale.java b/src/main/java/com/gmail/nossr50/locale/mcLocale.java index 65c85473f..588a6abe0 100644 --- a/src/main/java/com/gmail/nossr50/locale/mcLocale.java +++ b/src/main/java/com/gmail/nossr50/locale/mcLocale.java @@ -21,7 +21,7 @@ public class mcLocale { * Gets the appropriate string from the Locale files. * * @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 */ public static String getString(String key, Object[] messageArguments) { diff --git a/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java b/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java new file mode 100644 index 000000000..fb14fb002 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/runnables/SQLReconnect.java @@ -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 + } + } + } + } +}