Migrate to HikariCP rather than raw drivers, close #40

This commit is contained in:
William committed 2021-10-02 22:24:47 +01:00
1 parent b026fbcd0a
commit 894b50ad0d
7 files changed
+127 -42

No files matched your search

+10
View File
@@ -51,6 +51,10 @@
<pattern>redis.clients.jedis</pattern> <pattern>redis.clients.jedis</pattern>
<shadedPattern>me.william278.huskhomes2.libraries.jedis</shadedPattern> <shadedPattern>me.william278.huskhomes2.libraries.jedis</shadedPattern>
</relocation> </relocation>
<relocation>
<pattern>com.zaxxer.hikari</pattern>
<shadedPattern>me.william278.huskhomes2.libraries.hikaricp</shadedPattern>
</relocation>
</relocations> </relocations>
</configuration> </configuration>
<executions> <executions>
@@ -183,5 +187,11 @@
<type>jar</type> <type>jar</type>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
<version>5.0.0</version>
<scope>compile</scope>
</dependency>
</dependencies> </dependencies>
</project> </project>
@@ -40,6 +40,13 @@ public class Settings {
private String mySQLPassword; private String mySQLPassword;
private String mySQLParams; 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 // Redis connection settings
private int redisPort; private int redisPort;
private String redisHost; private String redisHost;
@@ -147,6 +154,12 @@ public class Settings {
this.mySQLPort = config.getInt("data_storage_options.mysql_credentials.port", 3306); 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.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.redisHost = config.getString("bungee_options.redis_credentials.host", "localhost");
this.redisPort = config.getInt("bungee_options.redis_credentials.port", 6379); this.redisPort = config.getInt("bungee_options.redis_credentials.port", 6379);
this.redisPassword = config.getString("bungee_options.redis_credentials.password", ""); this.redisPassword = config.getString("bungee_options.redis_credentials.password", "");
@@ -478,4 +491,24 @@ public class Settings {
public String getRedisPassword() { public String getRedisPassword() {
return redisPassword; 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;
}
} }
@@ -21,6 +21,12 @@ public abstract class Database {
public abstract void backup(); 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() { public void initialize() {
Connection connection = getConnection(); Connection connection = getConnection();
@@ -1,9 +1,9 @@
package me.william278.huskhomes2.data.SQL; package me.william278.huskhomes2.data.SQL;
import com.zaxxer.hikari.HikariDataSource;
import me.william278.huskhomes2.HuskHomes; import me.william278.huskhomes2.HuskHomes;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.util.logging.Level; import java.util.logging.Level;
@@ -71,7 +71,7 @@ public class MySQL extends Database {
final String password = HuskHomes.getSettings().getMySQLPassword(); final String password = HuskHomes.getSettings().getMySQLPassword();
final String params = HuskHomes.getSettings().getMySQLParams(); final String params = HuskHomes.getSettings().getMySQLParams();
private Connection connection; private HikariDataSource dataSource;
public MySQL(HuskHomes instance) { public MySQL(HuskHomes instance) {
super(instance); super(instance);
@@ -79,34 +79,47 @@ public class MySQL extends Database {
@Override @Override
public Connection getConnection() { public Connection getConnection() {
Connection connection = null;
try { try {
if (connection == null || connection.isClosed()) { if (dataSource == null || dataSource.isClosed()) {
try { try {
synchronized (HuskHomes.getInstance()) { // Create new HikariCP data source
Class.forName("com.mysql.cj.jdbc.Driver"); final String jdbcUrl = "jdbc:mysql://" + host + ":" + port + "/" + database + params;
connection = (DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + params, username, password)); HikariDataSource hikariDataSource = new HikariDataSource();
} hikariDataSource.setJdbcUrl(jdbcUrl);
} catch (SQLException ex) {
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the mySQL database: ", ex); hikariDataSource.setUsername(username);
} catch (ClassNotFoundException ex) { hikariDataSource.setPassword(password);
plugin.getLogger().log(Level.SEVERE, "The mySQL JBDC library is missing! Please download and place this in the /lib folder.");
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) { } catch (SQLException e) {
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception); plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
} }
return connection; return connection;
} }
@Override @Override
public void load() { public void load() {
connection = getConnection(); Connection connection = getConnection();
try(Statement statement = connection.createStatement()) { try(Statement statement = connection.createStatement()) {
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) { for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement); statement.execute(tableCreationStatement);
} }
} catch (SQLException e) { } 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(); initialize();
} }
@@ -1,18 +1,18 @@
package me.william278.huskhomes2.data.SQL; package me.william278.huskhomes2.data.SQL;
import com.zaxxer.hikari.HikariDataSource;
import me.william278.huskhomes2.HuskHomes; 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.Files;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import java.sql.Connection; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.Locale; import java.util.Locale;
import java.util.logging.Level; import java.util.logging.Level;
@@ -74,50 +74,65 @@ public class SQLite extends Database {
private static final String DATABASE_NAME = "HuskHomesData"; private static final String DATABASE_NAME = "HuskHomesData";
private Connection connection; private HikariDataSource dataSource;
public SQLite(HuskHomes instance) { public SQLite(HuskHomes instance) {
super(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 @Override
public Connection getConnection() { public Connection getConnection() {
Connection connection = null;
try { try {
if (connection == null || connection.isClosed()) { if (dataSource == null || dataSource.isClosed()) {
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db"); createDatabaseFileIfNotExist();
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() + ")");
}
}
try { try {
Class.forName("org.sqlite.JDBC"); final String jdbcUrl = "jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db";
connection = (DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db")); HikariDataSource hikariDataSource = new HikariDataSource();
} catch (SQLException ex) { hikariDataSource.setJdbcUrl(jdbcUrl);
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
} catch (ClassNotFoundException ex) { hikariDataSource.setMaximumPoolSize(hikariMaximumPoolSize);
plugin.getLogger().log(Level.SEVERE, "The SQLite JBDC library is missing! Please download and place this in the /lib folder."); 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) { } catch (SQLException e) {
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception); plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
} }
return connection; return connection;
} }
@Override @Override
public void load() { public void load() {
connection = getConnection(); Connection connection = getConnection();
try (Statement statement = connection.createStatement()) { try (Statement statement = connection.createStatement()) {
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) { for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
statement.execute(tableCreationStatement); statement.execute(tableCreationStatement);
} }
} catch (SQLException e) { } 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(); initialize();
} }
@@ -130,7 +145,9 @@ public class SQLite extends Database {
.withZone(ZoneId.systemDefault()) .withZone(ZoneId.systemDefault())
.format(Instant.now()).replaceAll(" ", "-") + ".db"; .format(Instant.now()).replaceAll(" ", "-") + ".db";
final File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".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); final File backUpFile = new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME + File.separator + backupFileName);
try { try {
Files.copy(databaseFile.toPath(), backUpFile.toPath(), StandardCopyOption.REPLACE_EXISTING); Files.copy(databaseFile.toPath(), backUpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
+6
View File
@@ -15,6 +15,12 @@ data_storage_options:
username: root username: root
password: pa55w0rd password: pa55w0rd
params: '?autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull' 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: table_names:
player_data: 'huskhomes_player_data' player_data: 'huskhomes_player_data'
locations_data: 'huskhomes_location_data' locations_data: 'huskhomes_location_data'
+1 -1
View File
@@ -5,7 +5,7 @@ api-version: 1.16
authors: [William278] authors: [William278]
softdepend: [dynmap, Vault, BlueMap, Pl3xMap] softdepend: [dynmap, Vault, BlueMap, Pl3xMap]
description: A powerful, intuitive and flexible teleportation suite 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: commands:
home: home: