From 6beaf194ce93ef0ae21f7e71286717f15ab54c18 Mon Sep 17 00:00:00 2001 From: games647 Date: Tue, 2 Aug 2022 10:38:46 +0200 Subject: [PATCH] Let the JDBC DriveManager pick the right driver for us According to HikariCP, it's recommended to use the DataSource version if available. For MySQL, it's not recommended, because of bugs in the network timeout implementation. Therefore, we use here the jdbc url version still, but let the driver be picked by the DriveManager. Fixes #591 --- .../fastlogin/core/shared/FastLoginCore.java | 2 -- .../fastlogin/core/storage/MySQLStorage.java | 30 +++++++++++-------- .../fastlogin/core/storage/SQLStorage.java | 5 +--- .../fastlogin/core/storage/SQLiteStorage.java | 13 ++++---- 4 files changed, 27 insertions(+), 23 deletions(-) diff --git a/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java b/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java index 5e968199..fbe7fa8e 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java @@ -230,8 +230,6 @@ public class FastLoginCore

> { } HikariConfig databaseConfig = new HikariConfig(); - databaseConfig.setDriverClassName(driver); - String database = config.getString("database"); databaseConfig.setConnectionTimeout(config.getInt("timeout", 30) * 1_000L); diff --git a/core/src/main/java/com/github/games647/fastlogin/core/storage/MySQLStorage.java b/core/src/main/java/com/github/games647/fastlogin/core/storage/MySQLStorage.java index 2bbae0b4..868f7fbe 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/storage/MySQLStorage.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/storage/MySQLStorage.java @@ -30,23 +30,18 @@ import com.zaxxer.hikari.HikariConfig; public class MySQLStorage extends SQLStorage { + private static final String JDBC_PROTOCOL = "jdbc:"; + public MySQLStorage(FastLoginCore core, String driver, String host, int port, String database, HikariConfig config, boolean useSSL) { - super(core, - buildJDBCUrl(driver, host, port, database), - setParams(config, useSSL)); + super(core, setParams(config, driver, host, port, database, useSSL)); } - private static String buildJDBCUrl(String driver, String host, int port, String database) { - String protocol = "mysql"; - if (driver.contains("mariadb")) { - protocol = "mariadb"; - } + private static HikariConfig setParams(HikariConfig config, + String driver, String host, int port, String database, + boolean useSSL) { + config.setDriverClassName(driver); - return protocol + "://" + host + ':' + port + '/' + database; - } - - private static HikariConfig setParams(HikariConfig config, boolean useSSL) { // Require SSL on the server if requested in config - this will also verify certificate // Those values are deprecated in favor of sslMode config.addDataSourceProperty("useSSL", useSSL); @@ -56,11 +51,22 @@ public class MySQLStorage extends SQLStorage { // could be useful for hiding server details config.addDataSourceProperty("paranoid", true); + config.setJdbcUrl(JDBC_PROTOCOL + buildJDBCUrl(driver, host, port, database)); + // enable MySQL specific optimizations addPerformanceProperties(config); return config; } + private static String buildJDBCUrl(String driver, String host, int port, String database) { + String protocol = "mysql"; + if (driver.contains("mariadb")) { + protocol = "mariadb"; + } + + return protocol + "://" + host + ':' + port + '/' + database; + } + private static void addPerformanceProperties(HikariConfig config) { // disabled by default - will return the same prepared statement instance config.addDataSourceProperty("cachePrepStmts", true); diff --git a/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java b/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java index 7b1fa631..dff841bf 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java @@ -45,8 +45,6 @@ import static java.sql.Statement.RETURN_GENERATED_KEYS; public abstract class SQLStorage implements AuthStorage { - private static final String JDBC_PROTOCOL = "jdbc:"; - protected static final String PREMIUM_TABLE = "premium"; protected static final String CREATE_TABLE_STMT = "CREATE TABLE IF NOT EXISTS `" + PREMIUM_TABLE + "` (" + "`UserID` INTEGER PRIMARY KEY AUTO_INCREMENT, " @@ -70,7 +68,7 @@ public abstract class SQLStorage implements AuthStorage { protected final FastLoginCore core; protected final HikariDataSource dataSource; - public SQLStorage(FastLoginCore core, String jdbcURL, HikariConfig config) { + public SQLStorage(FastLoginCore core, HikariConfig config) { this.core = core; config.setPoolName(core.getPlugin().getName()); @@ -79,7 +77,6 @@ public abstract class SQLStorage implements AuthStorage { config.setThreadFactory(platformThreadFactory); } - config.setJdbcUrl(JDBC_PROTOCOL + jdbcURL); this.dataSource = new HikariDataSource(config); } diff --git a/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLiteStorage.java b/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLiteStorage.java index 41beb594..c57dec32 100644 --- a/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLiteStorage.java +++ b/core/src/main/java/com/github/games647/fastlogin/core/storage/SQLiteStorage.java @@ -39,19 +39,22 @@ import java.util.concurrent.locks.ReentrantLock; public class SQLiteStorage extends SQLStorage { + private static final String SQLITE_DRIVER = "org.sqlite.SQLiteDataSource"; private final Lock lock = new ReentrantLock(); public SQLiteStorage(FastLoginCore core, String databasePath, HikariConfig config) { - super(core, - "sqlite://" + replacePathVariables(core.getPlugin(), databasePath), - setParams(config)); + super(core, setParams(config, replacePathVariables(core.getPlugin(), databasePath))); } - private static HikariConfig setParams(HikariConfig config) { + private static HikariConfig setParams(HikariConfig config, String path) { + config.setDataSourceClassName(SQLITE_DRIVER); + config.setConnectionTestQuery("SELECT 1"); config.setMaximumPoolSize(1); - //a try to fix https://www.spigotmc.org/threads/fastlogin.101192/page-26#post-1874647 + config.addDataSourceProperty("databaseName", path); + + // a try to fix https://www.spigotmc.org/threads/fastlogin.101192/page-26#post-1874647 // format strings retrieved by the timestamp column to match them from MySQL config.addDataSourceProperty("date_string_format", "yyyy-MM-dd HH:mm:ss");