Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package de.myzelyam.premiumvanish.database;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.util.logging.Level;
- import de.myzelyam.premiumvanish.utilities.Annotations.SoftwareIndependent;
- import de.myzelyam.premiumvanish.utilities.Annotations.ThreadSafe;
- import de.myzelyam.premiumvanish.utilities.ExceptionHandler;
- import de.myzelyam.premiumvanish.utilities.PluginUtils;
- /**
- * Represents a thread-safe connection to a MySQL-Database, however, queries
- * must be synchronized on the lock object in this class
- * */
- @SoftwareIndependent
- @ThreadSafe
- public final class SQLDatabase {
- private Connection con;
- private final String ip, host, user, password, database;
- private PluginUtils utils;
- public final Object lock = new Object();
- public SQLDatabase(String ip, String host, String database, String user,
- String password, PluginUtils utils) throws DatabaseException {
- this.utils = utils;
- this.ip = ip;
- this.host = host;
- this.database = database;
- this.user = user;
- this.password = password;
- try {
- Class.forName("com.mysql.jdbc.Driver");
- con = DriverManager.getConnection(
- "jdbc:mysql://" + ip + (host.equals("") ? "" : ":") + host
- + "/" + database, user, password);
- } catch (SQLException | ClassNotFoundException e) {
- throw new DatabaseException(e);
- }
- }
- /**
- *
- * @param The
- * SQL-Command to execute
- * @return a ResultSet object that contains the data produced by the given
- * query; never null
- * @throws DatabaseException
- * if a SQLException occurred or the operation failed badly
- */
- public ResultSet executeQuery(String cmd) throws DatabaseException {
- synchronized (lock) {
- try {
- if (con.isClosed()) {
- reconnect();
- }
- return con.createStatement().executeQuery(cmd);
- } catch (SQLException e) {
- try {
- if (con.isClosed()) {
- if (reconnect()) {
- return con.createStatement().executeQuery(cmd);
- }
- }
- } catch (SQLException e1) {
- throw new DatabaseException(e1);
- }
- throw new DatabaseException(e);
- }
- }
- }
- /**
- *
- * Executes an update async, onFail will be executed if an exception
- * occurred and can be null.
- */
- public void executeUpdateAsync(final String cmd,
- final ExceptionHandler onFail) {
- utils.scheduleAsync(new Runnable() {
- @Override
- public void run() {
- try {
- executeUpdate(cmd);
- } catch (DatabaseException e) {
- if (onFail != null)
- onFail.handle(e);
- }
- }
- });
- }
- /**
- *
- * @return either (1) the row count for SQL Data Manipulation Language (DML)
- * statements or (2) 0 for SQL statements that return nothing
- */
- public int executeUpdate(String cmd) throws DatabaseException {
- synchronized (lock) {
- try {
- if (con.isClosed()) {
- reconnect();
- }
- return con.createStatement().executeUpdate(cmd);
- } catch (SQLException e) {
- try {
- if (con.isClosed()) {
- if (reconnect())
- return con.createStatement().executeUpdate(cmd);
- }
- } catch (SQLException e1) {
- throw new DatabaseException(e1);
- }
- throw new DatabaseException(e);
- }
- }
- }
- public Connection getConnection() {
- return con;
- }
- /**
- * @return TRUE if the attempt to reconnect was successful, FALSE otherwise
- */
- private boolean reconnect() {
- synchronized (lock) {
- try {
- con = DriverManager.getConnection(
- "jdbc:mysql://" + ip + (host.equals("") ? "" : ":")
- + host + "/" + database, user, password);
- return true;
- } catch (SQLException er) {
- utils.log(Level.SEVERE,
- "[PremiumVanish] FATAL: Failed to reconnect to the database.");
- }
- return false;
- }
- }
- public void close() {
- synchronized (lock) {
- try {
- if (con != null)
- con.close();
- } catch (SQLException e) {
- utils.logException(e);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment