Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1. /*
  2. * Source: Main.cs
  3. * Project: DBUtil
  4. * Description: Stores mostly abstract classes and classes that
  5. * need to be invoked at runtime (for example the MySQL connector).
  6. * Author: Kane Holbrook (4/May/2011)
  7. */
  8.  
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Text;
  13. using MySql.Data.MySqlClient;
  14. using System.Timers;
  15.  
  16. namespace DBUtil
  17. {
  18. // -- ----
  19. // DBUtil.MySQL <: Permissable
  20. // public static class
  21. //
  22. // Provides a set of methods and variables that store the MySQL
  23. // runtime environment. All variables are constant.
  24. public static class MySQL
  25. {
  26. public const string hostname = "192.168.12.50"; // Host of the MySQL hostname.
  27. public const string username = "dbutil"; // Username to use.
  28. public const string password = "nulled"; // Password.
  29. public const string database = "dbutil"; // The database itself.
  30.  
  31. // DBUtil.MySQL.IsAlive
  32. // static public bool (property)
  33. // Returns the state of the MySQL connection. "False" if not connected or connection is lost, otherwise true.
  34. static public bool IsAlive = false;
  35.  
  36. // DBUtil.MySQL.Connection
  37. // static public MySqlConnection
  38. // Returns the MySQL connection. Should be used in conjunction with IsAlive to avoid returning a null value.
  39. static public MySqlConnection Connection;
  40.  
  41. // This timer is used to call MaintainConnection.
  42. static private Timer Timer;
  43.  
  44.  
  45. // -- ----
  46. // DBUtil.MySQL.Connect
  47. // static public bool method
  48. // Builds the connection string and connects to MySQL.
  49.  
  50. static public bool Connect()
  51. {
  52. // Build the connection string.
  53. Connection = new MySqlConnection(@"Server=" + hostname + "; Database=" + database + "; Uid=" + username + "; Pwd=" + password + ";");
  54.  
  55. try
  56. {
  57. // OPEN THOU CONNECTION.
  58. Connection.Open();
  59.  
  60. // If we get to here, we can assume MySQL connected successfully; so we start a timer to ping it every 15 seconds
  61. // in order to maintain connectivity.
  62. Timer.Elapsed += new ElapsedEventHandler(MaintainConnection);
  63. Timer.Interval = 15000;
  64. Timer.Start();
  65.  
  66. IsAlive = true;
  67.  
  68. // THOU IS CONNECTIF'.
  69. return true;
  70. }
  71. catch (MySqlException E)
  72. {
  73. // aww fuck. :(
  74. Console.WriteLine("[DBUtil.DLL] Failed to connect to MySQL database. Reason specified:\r\n\t" + E.ToString());
  75. Console.WriteLine("[DBUtil.DLL] Layman's terms: you probably entered the wrong details. Dumb fuck.");
  76. return false;
  77. }
  78. }
  79.  
  80. // This function is hooked to a timer; its purpose is to maintain the MySQL connection even when it goes down.
  81. // If it detects that connectivity has been lost it will attempt to reestablish it.
  82. static private void MaintainConnection(object sender, ElapsedEventArgs e)
  83. {
  84. // Ping return false if the connection is lost.
  85. if (!Connection.Ping())
  86. {
  87. // So we stop the timer to stop us getting ten thousand instances of it while we try to reconnect.
  88. Timer.Stop();
  89.  
  90. // And let other applications know that the SQL has died.
  91. IsAlive = false;
  92.  
  93. // And we try the reconnection sequence five times.
  94. for (int i = 0; i < 5; i++)
  95. {
  96. // We inform the user and close the broken connection, ready to be restarted..
  97. Console.WriteLine("[DBUtil.DLL] Connection lost to MySQL database. Attempting to reconnect ... attempt " + i);
  98. Connection.Close();
  99.  
  100. try
  101. {
  102. // And we then try to reconnect.
  103. Connection.Open();
  104.  
  105. // If no exception has been thrown by now you can assume we are connected. So we
  106. // restart the ping timer (that coincidently calls this function) and inform the user
  107.  
  108. Timer.Start();
  109. IsAlive = true;
  110. Console.WriteLine("[DBUtil.DLL] Connection re-established.");
  111. return;
  112. }
  113. catch (Exception E)
  114. {
  115. // Throw an exception and try again.
  116. Console.WriteLine("[DBUtil.DLL] Failed to reconnect; reason specified: " + E.ToString());
  117. }
  118.  
  119. // Then sleep for a second then try again.
  120. System.Threading.Thread.Sleep(1000);
  121. }
  122. }
  123. }
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement