Guest User

Untitled

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