diff --git a/pom.xml b/pom.xml
index 7c6496e6..a851598a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -51,6 +51,10 @@
redis.clients.jedis
me.william278.huskhomes2.libraries.jedis
+
+ com.zaxxer.hikari
+ me.william278.huskhomes2.libraries.hikaricp
+
@@ -183,5 +187,11 @@
jar
compile
+
+ com.zaxxer
+ HikariCP
+ 5.0.0
+ compile
+
diff --git a/src/main/java/me/william278/huskhomes2/config/Settings.java b/src/main/java/me/william278/huskhomes2/config/Settings.java
index c5264223..30b0adeb 100644
--- a/src/main/java/me/william278/huskhomes2/config/Settings.java
+++ b/src/main/java/me/william278/huskhomes2/config/Settings.java
@@ -40,6 +40,13 @@ public class Settings {
private String mySQLPassword;
private String mySQLParams;
+ // Hikari connection pool options
+ private int hikariMaximumPoolSize;
+ private int hikariMinimumIdle;
+ private long hikariMaximumLifetime;
+ private long hikariKeepAliveTime;
+ private long hikariConnectionTimeOut;
+
// Redis connection settings
private int redisPort;
private String redisHost;
@@ -147,6 +154,12 @@ public class Settings {
this.mySQLPort = config.getInt("data_storage_options.mysql_credentials.port", 3306);
this.mySQLParams = config.getString("data_storage_options.mysql_credentials.params", "?autoReconnect=true&useSSL=false");
+ this.hikariMaximumPoolSize = config.getInt("data_storage_options.connection_pool_options.maximum_pool_size", 10);
+ this.hikariMinimumIdle = config.getInt("data_storage_options.connection_pool_options.minimum_idle", 10);
+ this.hikariMaximumLifetime = config.getLong("data_storage_options.connection_pool_options.maximum_lifetime", 1800000);
+ this.hikariKeepAliveTime = config.getLong("data_storage_options.connection_pool_options.keepalive_time", 0);
+ this.hikariConnectionTimeOut = config.getLong("data_storage_options.connection_pool_options.connection_timeout", 5000);
+
this.redisHost = config.getString("bungee_options.redis_credentials.host", "localhost");
this.redisPort = config.getInt("bungee_options.redis_credentials.port", 6379);
this.redisPassword = config.getString("bungee_options.redis_credentials.password", "");
@@ -478,4 +491,24 @@ public class Settings {
public String getRedisPassword() {
return redisPassword;
}
+
+ public int getHikariMaximumPoolSize() {
+ return hikariMaximumPoolSize;
+ }
+
+ public int getHikariMinimumIdle() {
+ return hikariMinimumIdle;
+ }
+
+ public long getHikariMaximumLifetime() {
+ return hikariMaximumLifetime;
+ }
+
+ public long getHikariKeepAliveTime() {
+ return hikariKeepAliveTime;
+ }
+
+ public long getHikariConnectionTimeOut() {
+ return hikariConnectionTimeOut;
+ }
}
diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/Database.java b/src/main/java/me/william278/huskhomes2/data/SQL/Database.java
index 2fcbb549..b4f9eaf7 100644
--- a/src/main/java/me/william278/huskhomes2/data/SQL/Database.java
+++ b/src/main/java/me/william278/huskhomes2/data/SQL/Database.java
@@ -21,6 +21,12 @@ public abstract class Database {
public abstract void backup();
+ public final int hikariMaximumPoolSize = HuskHomes.getSettings().getHikariMaximumPoolSize();
+ public final int hikariMinimumIdle = HuskHomes.getSettings().getHikariMinimumIdle();
+ 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();
diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java
index 1140e042..97851a42 100644
--- a/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java
+++ b/src/main/java/me/william278/huskhomes2/data/SQL/MySQL.java
@@ -1,9 +1,9 @@
package me.william278.huskhomes2.data.SQL;
+import com.zaxxer.hikari.HikariDataSource;
import me.william278.huskhomes2.HuskHomes;
import java.sql.Connection;
-import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.logging.Level;
@@ -71,7 +71,7 @@ public class MySQL extends Database {
final String password = HuskHomes.getSettings().getMySQLPassword();
final String params = HuskHomes.getSettings().getMySQLParams();
- private Connection connection;
+ private HikariDataSource dataSource;
public MySQL(HuskHomes instance) {
super(instance);
@@ -79,34 +79,47 @@ public class MySQL extends Database {
@Override
public Connection getConnection() {
+ Connection connection = null;
try {
- if (connection == null || connection.isClosed()) {
+ if (dataSource == null || dataSource.isClosed()) {
try {
- synchronized (HuskHomes.getInstance()) {
- Class.forName("com.mysql.cj.jdbc.Driver");
- connection = (DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + params, username, password));
- }
- } catch (SQLException ex) {
- plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the mySQL database: ", ex);
- } catch (ClassNotFoundException ex) {
- plugin.getLogger().log(Level.SEVERE, "The mySQL JBDC library is missing! Please download and place this in the /lib folder.");
+ // 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 exception) {
- plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
+ } catch (SQLException e) {
+ plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
}
return connection;
}
@Override
public void load() {
- connection = getConnection();
+ Connection connection = 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: ", e);
+ plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables on the MySQL database: ", e);
}
initialize();
}
diff --git a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java
index 7db0ecba..750b3d18 100644
--- a/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java
+++ b/src/main/java/me/william278/huskhomes2/data/SQL/SQLite.java
@@ -1,18 +1,18 @@
package me.william278.huskhomes2.data.SQL;
+import com.zaxxer.hikari.HikariDataSource;
import me.william278.huskhomes2.HuskHomes;
-import java.io.*;
+import java.io.File;
+import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
-import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
-import java.time.format.FormatStyle;
import java.util.Locale;
import java.util.logging.Level;
@@ -74,50 +74,65 @@ public class SQLite extends Database {
private static final String DATABASE_NAME = "HuskHomesData";
- private Connection connection;
+ private HikariDataSource dataSource;
public SQLite(HuskHomes instance) {
super(instance);
}
+ private void createDatabaseFileIfNotExist() {
+ File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
+ if (!databaseFile.exists()) {
+ try {
+ if (!databaseFile.createNewFile()) {
+ plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + DATABASE_NAME + ".db (file already exists)");
+ }
+ } catch (IOException e) {
+ plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
+ }
+ }
+ }
+
@Override
public Connection getConnection() {
+ Connection connection = null;
try {
- if (connection == null || connection.isClosed()) {
- File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
- if (!databaseFile.exists()) {
- try {
- if (!databaseFile.createNewFile()) {
- plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + DATABASE_NAME + ".db (file already exists)");
- }
- } catch (IOException e) {
- plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
- }
- }
+ if (dataSource == null || dataSource.isClosed()) {
+ createDatabaseFileIfNotExist();
try {
- Class.forName("org.sqlite.JDBC");
- connection = (DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db"));
- } catch (SQLException ex) {
- plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
- } catch (ClassNotFoundException ex) {
- plugin.getLogger().log(Level.SEVERE, "The SQLite JBDC library is missing! Please download and place this in the /lib folder.");
+ 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 exception) {
- plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
+ } catch (SQLException e) {
+ plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
}
return connection;
}
@Override
public void load() {
- connection = getConnection();
+ Connection connection = 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: ", e);
+ plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables on the SQLite database: ", e);
}
initialize();
}
@@ -130,7 +145,9 @@ public class SQLite extends Database {
.withZone(ZoneId.systemDefault())
.format(Instant.now()).replaceAll(" ", "-") + ".db";
final File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
- new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME).mkdirs();
+ if (new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME).mkdirs()) {
+ plugin.getLogger().info("Created backups directory in HuskHomes plugin data folder.");
+ }
final File backUpFile = new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME + File.separator + backupFileName);
try {
Files.copy(databaseFile.toPath(), backUpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml
index 34c8a6c5..8e4fd7a6 100644
--- a/src/main/resources/config.yml
+++ b/src/main/resources/config.yml
@@ -15,6 +15,12 @@ data_storage_options:
username: root
password: pa55w0rd
params: '?autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull'
+ connection_pool_options:
+ maximum_pool_size: 10
+ minimum_idle: 10
+ maximum_lifetime: 1800000
+ keepalive_time: 0
+ connection_timeout: 5000
table_names:
player_data: 'huskhomes_player_data'
locations_data: 'huskhomes_location_data'
diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml
index ee040941..48d66bb4 100644
--- a/src/main/resources/plugin.yml
+++ b/src/main/resources/plugin.yml
@@ -5,7 +5,7 @@ api-version: 1.16
authors: [William278]
softdepend: [dynmap, Vault, BlueMap, Pl3xMap]
description: A powerful, intuitive and flexible teleportation suite
-libraries: [mysql:mysql-connector-java:8.0.25]
+libraries: [mysql:mysql-connector-java:8.0.25] # todo test removing this
commands:
home: