Restructure connection interfacing system

This commit is contained in:
William committed 2021-10-02 23:07:45 +01:00
1 parent 894b50ad0d
commit 7ef9159daf
33 files changed
+200 -256

No files matched your search

@@ -3,10 +3,7 @@ package me.william278.huskhomes2.data.SQL;
import me.william278.huskhomes2.HuskHomes;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
public abstract class Database {
protected HuskHomes plugin;
@@ -15,7 +12,7 @@ public abstract class Database {
plugin = instance;
}
public abstract Connection getConnection();
public abstract Connection getConnection() throws SQLException;
public abstract void load();
@@ -26,31 +23,4 @@ public abstract class Database {
public final long hikariMaximumLifetime = HuskHomes.getSettings().getHikariMaximumLifetime();
public final long hikariKeepAliveTime = HuskHomes.getSettings().getHikariKeepAliveTime();
public final long hikariConnectionTimeOut = HuskHomes.getSettings().getHikariConnectionTimeOut();
public void initialize() {
Connection connection = getConnection();
// Test the retrieved connection; throw an error if it fails
try {
PreparedStatement ps = connection.prepareStatement("SELECT * FROM " + HuskHomes.getSettings().getPlayerDataTable() + ";");
ResultSet rs = ps.executeQuery();
close(ps, rs);
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to retrieve Database connection: ", ex);
}
}
// Close the mySQL connection
public void close(PreparedStatement ps, ResultSet rs) {
try {
if (ps != null)
ps.close();
if (rs != null)
rs.close();
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "Failed to close the Database connection: ", ex);
}
}
}
@@ -78,50 +78,38 @@ public class MySQL extends Database {
}
@Override
public Connection getConnection() {
Connection connection = null;
try {
if (dataSource == null || dataSource.isClosed()) {
try {
// Create new HikariCP data source
final String jdbcUrl = "jdbc:mysql://" + host + ":" + port + "/" + database + params;
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setUsername(username);
hikariDataSource.setPassword(password);
hikariDataSource.setMaximumPoolSize(hikariMaximumPoolSize);
hikariDataSource.setMinimumIdle(hikariMinimumIdle);
hikariDataSource.setMaxLifetime(hikariMaximumLifetime);
hikariDataSource.setKeepaliveTime(hikariKeepAliveTime);
hikariDataSource.setConnectionTimeout(hikariConnectionTimeOut);
this.dataSource = hikariDataSource;
connection = dataSource.getConnection();
} catch (SQLException e ) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred initialising a new connection via HikariCP", e);
}
} else {
connection = dataSource.getConnection();
}
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
}
return connection;
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
@Override
public void load() {
Connection connection = getConnection();
try(Statement statement = connection.createStatement()) {
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement);
// Create new HikariCP data source
final String jdbcUrl = "jdbc:mysql://" + host + ":" + port + "/" + database + params;
dataSource = new HikariDataSource();
dataSource.setJdbcUrl(jdbcUrl);
dataSource.setUsername(username);
dataSource.setPassword(password);
// Set various additional parameters
dataSource.setMaximumPoolSize(hikariMaximumPoolSize);
dataSource.setMinimumIdle(hikariMinimumIdle);
dataSource.setMaxLifetime(hikariMaximumLifetime);
dataSource.setKeepaliveTime(hikariKeepAliveTime);
dataSource.setConnectionTimeout(hikariConnectionTimeOut);
dataSource.setPoolName("HuskHomesHikariPool");
// Create tables
try(Connection connection = dataSource.getConnection()) {
try(Statement statement = connection.createStatement()) {
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement);
}
}
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables on the MySQL database: ", e);
}
initialize();
}
@Override
@@ -80,6 +80,7 @@ public class SQLite extends Database {
super(instance);
}
// Create the database file if it does not exist yet
private void createDatabaseFileIfNotExist() {
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
if (!databaseFile.exists()) {
@@ -94,47 +95,38 @@ public class SQLite extends Database {
}
@Override
public Connection getConnection() {
Connection connection = null;
try {
if (dataSource == null || dataSource.isClosed()) {
createDatabaseFileIfNotExist();
try {
final String jdbcUrl = "jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db";
HikariDataSource hikariDataSource = new HikariDataSource();
hikariDataSource.setJdbcUrl(jdbcUrl);
hikariDataSource.setMaximumPoolSize(hikariMaximumPoolSize);
hikariDataSource.setMinimumIdle(hikariMinimumIdle);
hikariDataSource.setMaxLifetime(hikariMaximumLifetime);
hikariDataSource.setKeepaliveTime(hikariKeepAliveTime);
hikariDataSource.setConnectionTimeout(hikariConnectionTimeOut);
this.dataSource = hikariDataSource;
connection = dataSource.getConnection();
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred initialising a new connection via HikariCP", e);
}
} else {
connection = dataSource.getConnection();
}
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
}
return connection;
public Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
@Override
public void load() {
Connection connection = getConnection();
try (Statement statement = connection.createStatement()) {
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement);
// Make SQLite database file
createDatabaseFileIfNotExist();
// Create new HikariCP data source
final String jdbcUrl = "jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db";
dataSource = new HikariDataSource();
dataSource.setJdbcUrl(jdbcUrl);
// Set various additional parameters
dataSource.setMaximumPoolSize(hikariMaximumPoolSize);
dataSource.setMinimumIdle(hikariMinimumIdle);
dataSource.setMaxLifetime(hikariMaximumLifetime);
dataSource.setKeepaliveTime(hikariKeepAliveTime);
dataSource.setConnectionTimeout(hikariConnectionTimeOut);
dataSource.setPoolName("HuskHomesHikariPool");
// Create tables
try(Connection connection = dataSource.getConnection()) {
try(Statement statement = connection.createStatement()) {
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement);
}
}
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables on the SQLite database: ", e);
}
initialize();
}
@Override
@@ -23,10 +23,9 @@ public class CrossServerMessageHandler {
private static final HuskHomes plugin = HuskHomes.getInstance();
public static void handlePluginMessage(Message receivedMessage, Player recipient) {
Connection connection = HuskHomes.getConnection();
switch (receivedMessage.getMessageType()) {
case SET_TP_DESTINATION -> Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
try (Connection connection = HuskHomes.getConnection()) {
DataManager.setPlayerDestinationLocation(receivedMessage.getMessageData(),
new TeleportationPoint(recipient.getLocation(), HuskHomes.getSettings().getServerID()), connection);
new PluginMessage(receivedMessage.getMessageData(), Message.MessageType.CONFIRM_DESTINATION_SET, "confirmed").send(recipient);
@@ -63,7 +62,7 @@ public class CrossServerMessageHandler {
if (tpaAccepted) {
MessageManager.sendMessage(recipient, "tpa_has_accepted", tpaReplierName);
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
try (Connection connection = HuskHomes.getConnection()) {
TeleportManager.queueTimedTeleport(recipient, tpaReplierName, connection);
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred responding to a plugin message teleport request reply");
@@ -83,7 +82,7 @@ public class CrossServerMessageHandler {
}
}
case TELEPORT_TO_ME -> Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
try (Connection connection = HuskHomes.getConnection()) {
TeleportManager.teleportPlayer(recipient, receivedMessage.getMessageData(), connection);
} catch (SQLException e) {
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred responding to a plugin message teleport-to-me request");