From b0de1b93f9ddc7a111d72ea03eae32ff486089a1 Mon Sep 17 00:00:00 2001 From: Adam Frisby Date: Wed, 30 May 2007 02:36:48 +0000 Subject: * Added reconnect support for MySQL Data interfaces if they time out. (Grid/User modes only now, Log todo) * Begun writing support for supporting the "Start" region login parameter. --- .../OpenGrid.Framework.Data.MySQL/MySQLGridData.cs | 3 + .../OpenGrid.Framework.Data.MySQL/MySQLManager.cs | 68 ++++++++++++++++++++-- .../OpenGrid.Framework.Data.MySQL/MySQLUserData.cs | 3 + .../OpenGridServices.UserServer/UserManager.cs | 17 +++++- 4 files changed, 85 insertions(+), 6 deletions(-) (limited to 'OpenGridServices') diff --git a/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLGridData.cs b/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLGridData.cs index 6ac8cc3..5006aaf 100644 --- a/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLGridData.cs +++ b/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLGridData.cs @@ -75,6 +75,7 @@ namespace OpenGrid.Framework.Data.MySQL } catch (Exception e) { + database.Reconnect(); Console.WriteLine(e.ToString()); return null; } @@ -106,6 +107,7 @@ namespace OpenGrid.Framework.Data.MySQL } catch (Exception e) { + database.Reconnect(); Console.WriteLine(e.ToString()); return null; } @@ -137,6 +139,7 @@ namespace OpenGrid.Framework.Data.MySQL } catch (Exception e) { + database.Reconnect(); Console.WriteLine(e.ToString()); return null; } diff --git a/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLManager.cs b/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLManager.cs index aee18b9..2eaa158 100644 --- a/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLManager.cs +++ b/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLManager.cs @@ -16,6 +16,7 @@ namespace OpenGrid.Framework.Data.MySQL class MySQLManager { IDbConnection dbcon; + string connectionString; /// /// Initialises and creates a new MySQL connection and maintains it. @@ -29,7 +30,7 @@ namespace OpenGrid.Framework.Data.MySQL { try { - string connectionString = "Server=" + hostname + ";Port=" + port + ";Database=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=" + cpooling + ";"; + connectionString = "Server=" + hostname + ";Port=" + port + ";Database=" + database + ";User ID=" + username + ";Password=" + password + ";Pooling=" + cpooling + ";"; dbcon = new MySqlConnection(connectionString); dbcon.Open(); @@ -52,6 +53,28 @@ namespace OpenGrid.Framework.Data.MySQL } /// + /// Reconnects to the database + /// + public void Reconnect() + { + lock (dbcon) + { + try + { + // Close the DB connection + dbcon.Close(); + // Try reopen it + dbcon = new MySqlConnection(connectionString); + dbcon.Open(); + } + catch (Exception e) + { + Console.WriteLine("Unable to reconnect to database " + e.ToString()); + } + } + } + + /// /// Runs a query with protection against SQL Injection by using parameterised input. /// /// The SQL string - replace any variables such as WHERE x = "y" with WHERE x = @y @@ -70,10 +93,47 @@ namespace OpenGrid.Framework.Data.MySQL return (IDbCommand)dbcommand; } - catch (Exception e) + catch { - Console.WriteLine("Failed during Query generation: " + e.ToString()); - return null; + lock (dbcon) + { + // Close the DB connection + try + { + dbcon.Close(); + } + catch { } + + // Try reopen it + try + { + dbcon = new MySqlConnection(connectionString); + dbcon.Open(); + } + catch (Exception e) + { + Console.WriteLine("Unable to reconnect to database " + e.ToString()); + } + + // Run the query again + try + { + MySqlCommand dbcommand = (MySqlCommand)dbcon.CreateCommand(); + dbcommand.CommandText = sql; + foreach (KeyValuePair param in parameters) + { + dbcommand.Parameters.Add(param.Key, param.Value); + } + + return (IDbCommand)dbcommand; + } + catch (Exception e) + { + // Return null if it fails. + Console.WriteLine("Failed during Query generation: " + e.ToString()); + return null; + } + } } } diff --git a/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLUserData.cs b/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLUserData.cs index 57dbfc6..6f3cad6 100644 --- a/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLUserData.cs +++ b/OpenGridServices/OpenGrid.Framework.Data.MySQL/MySQLUserData.cs @@ -51,6 +51,7 @@ namespace OpenGrid.Framework.Data.MySQL } catch (Exception e) { + database.Reconnect(); Console.WriteLine(e.ToString()); return null; } @@ -78,6 +79,7 @@ namespace OpenGrid.Framework.Data.MySQL } catch (Exception e) { + database.Reconnect(); Console.WriteLine(e.ToString()); return null; } @@ -116,6 +118,7 @@ namespace OpenGrid.Framework.Data.MySQL } catch (Exception e) { + database.Reconnect(); Console.WriteLine(e.ToString()); return null; } diff --git a/OpenGridServices/OpenGridServices.UserServer/UserManager.cs b/OpenGridServices/OpenGridServices.UserServer/UserManager.cs index cd80621..21ecc50 100644 --- a/OpenGridServices/OpenGridServices.UserServer/UserManager.cs +++ b/OpenGridServices/OpenGridServices.UserServer/UserManager.cs @@ -395,10 +395,23 @@ namespace OpenGridServices.UserServer // If user specified additional start, use that if (requestData.ContainsKey("start")) { - string startLoc = (string)requestData["start"]; + string startLoc = ((string)requestData["start"]).Trim(); if (!(startLoc == "last" || startLoc == "home")) { - // Ignore it! Heh. + // Format: uri:Ahern&162&213&34 + try + { + string[] parts = startLoc.Remove(0, 4).Split('&'); + string region = parts[0]; + + //////////////////////////////////////////////////// + //SimProfile SimInfo = new SimProfile(); + //SimInfo = SimInfo.LoadFromGrid(theUser.currentAgent.currentHandle, _config.GridServerURL, _config.GridSendKey, _config.GridRecvKey); + } + catch (Exception e) + { + + } } } -- cgit v1.1