Skip to content

Commit

Permalink
possible fix to inactivity bug in MySQL
Browse files Browse the repository at this point in the history
  • Loading branch information
firefly2442 committed Sep 4, 2012
1 parent 8d94c91 commit 0f8c36b
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions Arma2NETMySQLPlugin/MySQL.cs
Expand Up @@ -11,6 +11,9 @@ public class MySQL : SQL
{
private MySqlConnection connection;
private object getCommandSync = new object();
private System.Threading.Thread staleConnectionThread = null;
private DateTime lastQuery = new DateTime();
private String connectString = null;

public MySQL()
{
Expand All @@ -32,10 +35,27 @@ public override void OpenConnection(string connectionString)
connection = new MySqlConnection(connectionString);
}

//save the connection string, we'll need this if the check thread ever closes down the connection
connectString = connectionString;

//update the last time a query has been run
lastQuery = DateTime.Now;

//start up thread that checks to see how long we've gone since running a command
//we run into problems if the MySQL connection is kept open for significantly long periods of time
if (staleConnectionThread == null) {
staleConnectionThread = new System.Threading.Thread(new System.Threading.ParameterizedThreadStart(checkConnectionThread));
staleConnectionThread.Name = "staleConnectionThread";
staleConnectionThread.Start();
} else if (staleConnectionThread.ThreadState != System.Threading.ThreadState.Running) {
staleConnectionThread.Start();
}

while (connection.State != System.Data.ConnectionState.Open)
{
try
{
//Logger.addMessage(Logger.LogType.Info, "Opening MySQL connection.");
connection.Open();
}
catch (Exception ex)
Expand All @@ -60,6 +80,32 @@ public override void CloseConnection()
}
}

private void checkConnectionThread(object obj)
{
//This thread runs every 5 minutes and checks to see when the last time we ran a query
//If it's been over 30 minutes, we close down the MySQL connection
while (true)
{
if (connection != null && connection.State == System.Data.ConnectionState.Open)
{
TimeSpan ts = DateTime.Now - lastQuery;
if (ts.Minutes > 30) {
//over 30 minutes, close it down
Logger.addMessage(Logger.LogType.Info, "30 minutes of inactivity have passed. Closing down MySQL connection.");
CloseConnection();
break;
}
}
//http://www.dotnetperls.com/sleep
//TODO: according to the above link, sleep does not use CPU cycles
//we need to make sure this is not significantly eating into CPU resources!
Thread.Sleep((60 * 5) * 1000);
}
//Logger.addMessage(Logger.LogType.Info, "checkConnectionThread closing down.");
//threads cannot be started up again, so we null this out, it will be opened back up later
staleConnectionThread = null;
}

private MySqlCommand GetCommand(string procedureName)
{
MySqlCommand cmd = null;
Expand All @@ -78,6 +124,10 @@ private MySqlCommand GetCommand(string procedureName)

public override IEnumerable<string[][]> RunProcedure(string procedure, string[] parameters, int maxResultSize)
{
if (connection == null) {
OpenConnection(connectString);
}

//Logger.addMessage(Logger.LogType.Info, "Started RunProcedure");
if (connection != null && connection.State == System.Data.ConnectionState.Open && procedure != null)
{
Expand Down Expand Up @@ -107,6 +157,10 @@ public override IEnumerable<string[][]> RunProcedure(string procedure, string[]

public override IEnumerable<string[][]> RunCommand(string mysql_command, int maxResultSize)
{
if (connection == null) {
OpenConnection(connectString);
}

//Logger.addMessage(Logger.LogType.Info, "Started RunCommand");
if (connection != null && connection.State == System.Data.ConnectionState.Open && mysql_command != null)
{
Expand All @@ -122,6 +176,9 @@ private string[][] RunOnDatabase(MySqlCommand command, int maxResultSize)
{
MySqlDataReader reader = null;

//update the last time a query has been run
lastQuery = DateTime.Now;

Boolean mysql_error = false;
try
{
Expand Down

0 comments on commit 0f8c36b

Please sign in to comment.