Guest User

Async MySQL Bukkit

a guest
Aug 23rd, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.98 KB | None | 0 0
  1. package de.myzelyam.premiumvanish.database;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.util.logging.Level;
  8.  
  9. import de.myzelyam.premiumvanish.utilities.Annotations.SoftwareIndependent;
  10. import de.myzelyam.premiumvanish.utilities.Annotations.ThreadSafe;
  11. import de.myzelyam.premiumvanish.utilities.ExceptionHandler;
  12. import de.myzelyam.premiumvanish.utilities.PluginUtils;
  13.  
  14. /**
  15.  * Represents a thread-safe connection to a MySQL-Database, however, queries
  16.  * must be synchronized on the lock object in this class
  17.  * */
  18. @SoftwareIndependent
  19. @ThreadSafe
  20. public final class SQLDatabase {
  21.  
  22.     private Connection con;
  23.  
  24.     private final String ip, host, user, password, database;
  25.  
  26.     private PluginUtils utils;
  27.  
  28.     public final Object lock = new Object();
  29.  
  30.     public SQLDatabase(String ip, String host, String database, String user,
  31.             String password, PluginUtils utils) throws DatabaseException {
  32.         this.utils = utils;
  33.         this.ip = ip;
  34.         this.host = host;
  35.         this.database = database;
  36.         this.user = user;
  37.         this.password = password;
  38.         try {
  39.             Class.forName("com.mysql.jdbc.Driver");
  40.             con = DriverManager.getConnection(
  41.                     "jdbc:mysql://" + ip + (host.equals("") ? "" : ":") + host
  42.                             + "/" + database, user, password);
  43.         } catch (SQLException | ClassNotFoundException e) {
  44.             throw new DatabaseException(e);
  45.         }
  46.     }
  47.  
  48.     /**
  49.      *
  50.      * @param The
  51.      *            SQL-Command to execute
  52.      * @return a ResultSet object that contains the data produced by the given
  53.      *         query; never null
  54.      * @throws DatabaseException
  55.      *             if a SQLException occurred or the operation failed badly
  56.      */
  57.     public ResultSet executeQuery(String cmd) throws DatabaseException {
  58.         synchronized (lock) {
  59.             try {
  60.                 if (con.isClosed()) {
  61.                     reconnect();
  62.                 }
  63.                 return con.createStatement().executeQuery(cmd);
  64.             } catch (SQLException e) {
  65.                 try {
  66.                     if (con.isClosed()) {
  67.                         if (reconnect()) {
  68.                             return con.createStatement().executeQuery(cmd);
  69.                         }
  70.                     }
  71.                 } catch (SQLException e1) {
  72.                     throw new DatabaseException(e1);
  73.                 }
  74.                 throw new DatabaseException(e);
  75.             }
  76.         }
  77.     }
  78.  
  79.     /**
  80.      *
  81.      * Executes an update async, onFail will be executed if an exception
  82.      * occurred and can be null.
  83.      */
  84.     public void executeUpdateAsync(final String cmd,
  85.             final ExceptionHandler onFail) {
  86.         utils.scheduleAsync(new Runnable() {
  87.  
  88.             @Override
  89.             public void run() {
  90.                 try {
  91.                     executeUpdate(cmd);
  92.                 } catch (DatabaseException e) {
  93.                     if (onFail != null)
  94.                         onFail.handle(e);
  95.                 }
  96.             }
  97.         });
  98.     }
  99.  
  100.     /**
  101.      *
  102.      * @return either (1) the row count for SQL Data Manipulation Language (DML)
  103.      *         statements or (2) 0 for SQL statements that return nothing
  104.      */
  105.     public int executeUpdate(String cmd) throws DatabaseException {
  106.         synchronized (lock) {
  107.             try {
  108.                 if (con.isClosed()) {
  109.                     reconnect();
  110.                 }
  111.                 return con.createStatement().executeUpdate(cmd);
  112.             } catch (SQLException e) {
  113.                 try {
  114.                     if (con.isClosed()) {
  115.                         if (reconnect())
  116.                             return con.createStatement().executeUpdate(cmd);
  117.                     }
  118.                 } catch (SQLException e1) {
  119.                     throw new DatabaseException(e1);
  120.                 }
  121.                 throw new DatabaseException(e);
  122.             }
  123.         }
  124.     }
  125.  
  126.     public Connection getConnection() {
  127.         return con;
  128.     }
  129.  
  130.     /**
  131.      * @return TRUE if the attempt to reconnect was successful, FALSE otherwise
  132.      */
  133.     private boolean reconnect() {
  134.         synchronized (lock) {
  135.             try {
  136.                 con = DriverManager.getConnection(
  137.                         "jdbc:mysql://" + ip + (host.equals("") ? "" : ":")
  138.                                 + host + "/" + database, user, password);
  139.                 return true;
  140.             } catch (SQLException er) {
  141.                 utils.log(Level.SEVERE,
  142.                         "[PremiumVanish] FATAL: Failed to reconnect to the database.");
  143.             }
  144.             return false;
  145.         }
  146.     }
  147.  
  148.     public void close() {
  149.         synchronized (lock) {
  150.             try {
  151.                 if (con != null)
  152.                     con.close();
  153.             } catch (SQLException e) {
  154.                 utils.logException(e);
  155.             }
  156.         }
  157.     }
  158. }
Advertisement
Add Comment
Please, Sign In to add comment