Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Source: Main.cs
- * Project: DBUtil
- * Description: Stores mostly abstract classes and classes that
- * need to be invoked at runtime (for example the MySQL connector).
- * Author: Kane Holbrook (4/May/2011)
- */
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MySql.Data.MySqlClient;
- using System.Timers;
- namespace DBUtil
- {
- // -- ----
- // DBUtil.MySQL <: Permissable
- // public static class
- //
- // Provides a set of methods and variables that store the MySQL
- // runtime environment. All variables are constant.
- public static class MySQL
- {
- public const string hostname = "192.168.12.50"; // Host of the MySQL hostname.
- public const string username = "dbutil"; // Username to use.
- public const string password = "Pa55word1"; // Password.
- public const string database = "dbutil"; // The database itself.
- // DBUtil.MySQL.IsAlive
- // static public bool (property)
- // Returns the state of the MySQL connection. "False" if not connected or connection is lost, otherwise true.
- static public bool IsAlive = false;
- // DBUtil.MySQL.Connection
- // static public MySqlConnection
- // Returns the MySQL connection. Should be used in conjunction with IsAlive to avoid returning a null value.
- static public MySqlConnection Connection;
- // This timer is used to call MaintainConnection.
- static private Timer Timer;
- // -- ----
- // DBUtil.MySQL.Connect
- // static public bool method
- // Builds the connection string and connects to MySQL.
- static public bool Connect()
- {
- // Build the connection string.
- Connection = new MySqlConnection(@"Server=" + hostname + "; Database=" + database + "; Uid=" + username + "; Pwd=" + password + ";");
- try
- {
- // OPEN THOU CONNECTION.
- Connection.Open();
- // If we get to here, we can assume MySQL connected successfully; so we start a timer to ping it every 15 seconds
- // in order to maintain connectivity.
- Timer.Elapsed += new ElapsedEventHandler(MaintainConnection);
- Timer.Interval = 15000;
- Timer.Start();
- IsAlive = true;
- // THOU IS CONNECTIF'.
- return true;
- }
- catch (MySqlException E)
- {
- // aww fuck. :(
- Console.WriteLine("[DBUtil.DLL] Failed to connect to MySQL database. Reason specified:\r\n\t" + E.ToString());
- Console.WriteLine("[DBUtil.DLL] Layman's terms: you probably entered the wrong details. Dumb fuck.");
- return false;
- }
- }
- // This function is hooked to a timer; its purpose is to maintain the MySQL connection even when it goes down.
- // If it detects that connectivity has been lost it will attempt to reestablish it.
- static private void MaintainConnection(object sender, ElapsedEventArgs e)
- {
- // Ping return false if the connection is lost.
- if (!Connection.Ping())
- {
- // So we stop the timer to stop us getting ten thousand instances of it while we try to reconnect.
- Timer.Stop();
- // And let other applications know that the SQL has died.
- IsAlive = false;
- // And we try the reconnection sequence five times.
- for (int i = 0; i < 5; i++)
- {
- // We inform the user and close the broken connection, ready to be restarted..
- Console.WriteLine("[DBUtil.DLL] Connection lost to MySQL database. Attempting to reconnect ... attempt " + i);
- Connection.Close();
- try
- {
- // And we then try to reconnect.
- Connection.Open();
- // If no exception has been thrown by now you can assume we are connected. So we
- // restart the ping timer (that coincidently calls this function) and inform the user
- Timer.Start();
- IsAlive = true;
- Console.WriteLine("[DBUtil.DLL] Connection re-established.");
- return;
- }
- catch (Exception E)
- {
- // Throw an exception and try again.
- Console.WriteLine("[DBUtil.DLL] Failed to reconnect; reason specified: " + E.ToString());
- }
- // Then sleep for a second then try again.
- System.Threading.Thread.Sleep(1000);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement