mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-04 22:16:22 +00:00
Migrate to HikariCP rather than raw drivers, close #40
This commit is contained in:
1 parent
b026fbcd0a
commit
894b50ad0d
7 files changed
+120
-35
No files matched your search
@@ -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);
|
||||||
|
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
} catch (SQLException ex) {
|
} else {
|
||||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the mySQL database: ", ex);
|
connection = dataSource.getConnection();
|
||||||
} catch (ClassNotFoundException ex) {
|
|
||||||
plugin.getLogger().log(Level.SEVERE, "The mySQL JBDC library is missing! Please download and place this in the /lib folder.");
|
|
||||||
}
|
}
|
||||||
}
|
} catch (SQLException e) {
|
||||||
} catch (SQLException exception) {
|
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
|
||||||
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
|
|
||||||
}
|
}
|
||||||
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,16 +74,13 @@ 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);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
private void createDatabaseFileIfNotExist() {
|
||||||
public Connection getConnection() {
|
|
||||||
try {
|
|
||||||
if (connection == null || connection.isClosed()) {
|
|
||||||
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
|
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
|
||||||
if (!databaseFile.exists()) {
|
if (!databaseFile.exists()) {
|
||||||
try {
|
try {
|
||||||
@@ -94,30 +91,48 @@ public class SQLite extends Database {
|
|||||||
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
|
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Connection getConnection() {
|
||||||
|
Connection connection = null;
|
||||||
try {
|
try {
|
||||||
Class.forName("org.sqlite.JDBC");
|
if (dataSource == null || dataSource.isClosed()) {
|
||||||
connection = (DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db"));
|
createDatabaseFileIfNotExist();
|
||||||
} catch (SQLException ex) {
|
try {
|
||||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
|
final String jdbcUrl = "jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db";
|
||||||
} catch (ClassNotFoundException ex) {
|
HikariDataSource hikariDataSource = new HikariDataSource();
|
||||||
plugin.getLogger().log(Level.SEVERE, "The SQLite JBDC library is missing! Please download and place this in the /lib folder.");
|
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) {
|
} 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);
|
||||||
|
|||||||
@@ -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'
|
||||||
|
|||||||
@@ -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:
|
||||||
|
|||||||
Reference in new issue
Block a user