Guest User

Untitled

a guest
Oct 21st, 2017
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. package server.util;
  2.  
  3. /*
  4. * Made By Josheh
  5. */
  6.  
  7. import java.io.FileInputStream;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.util.Properties;
  11. import java.util.logging.Level;
  12. import java.util.logging.Logger;
  13.  
  14. /**
  15. * Handles the connection to our database server
  16. *
  17. * @author Stuart
  18. *
  19. */
  20. public class Database {
  21.  
  22. private static final Logger LOG = Logger
  23. .getLogger(Database.class.getName());
  24. private static Properties props = new Properties();
  25. private static Connection conn = null;
  26. private static long lastUsed = System.currentTimeMillis();
  27.  
  28. /**
  29. * Create
  30. *
  31. * @param _props
  32. * database properties
  33. * @throws Exception
  34. * error creating a new connection
  35. */
  36. public static void init() throws Exception {
  37. LOG.log(Level.INFO, "initiating database connection...");
  38. try {
  39. FileInputStream fis = new FileInputStream("database.xml");
  40. props.loadFromXML(fis);
  41. } catch (Exception e) {
  42. LOG.log(Level.SEVERE, "error loading database properties", e);
  43. throw new Exception("error loading database properties");
  44. }
  45. connect();
  46. }
  47.  
  48. private static void connect() throws Exception {
  49. LOG.log(Level.INFO, "connecting to database...");
  50. try {
  51. Class.forName("com.mysql.jdbc.Driver").newInstance();
  52. } catch (Exception e) {
  53. LOG.log(Level.SEVERE, "error loading mysql driver", e);
  54. throw new Exception("error loading mysql driver");
  55. }
  56. try {
  57. conn = DriverManager.getConnection(
  58. "jdbc:mysql://" + props.getProperty("host") + ":"
  59. + props.getProperty("port") + "/"
  60. + props.getProperty("name"),
  61. props.getProperty("username"),
  62. props.getProperty("password"));
  63. } catch (Exception e) {
  64. LOG.log(Level.SEVERE, "error connecting to database", e);
  65. throw new Exception("error connecting to database "
  66. + e.getMessage());
  67. }
  68. }
  69.  
  70. /**
  71. * Get the database connection, renews the connection if the connection has
  72. * not been used for 5 minutes
  73. *
  74. * @return the connection
  75. * @throws Exception
  76. * error getting the connection
  77. */
  78. public static Connection getConnection() throws Exception {
  79. if (conn == null) {
  80. throw new Exception("connection is null");
  81. }
  82. if (System.currentTimeMillis() - lastUsed > 300000) {
  83. try {
  84. lastUsed = System.currentTimeMillis();
  85. conn.close();
  86. connect();
  87. } catch (Exception e) {
  88. LOG.log(Level.SEVERE, "error refreshing database connection", e);
  89. throw new Exception("error refreshing database connection");
  90. }
  91. }
  92. return conn;
  93. }
  94.  
  95. /**
  96. * Close the database connection
  97. *
  98. * @throws Exception
  99. * error closing the database connection
  100. */
  101. public static void close() throws Exception {
  102. if (conn == null) {
  103. throw new Exception("connection is null");
  104. }
  105. conn.close();
  106. }
  107.  
  108. }
Add Comment
Please, Sign In to add comment