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
This commit is contained in:
games647
2022-08-02 10:38:46 +02:00
parent 62d7320a6e
commit 6beaf194ce
4 changed files with 27 additions and 23 deletions

View File

@ -230,8 +230,6 @@ public class FastLoginCore<P extends C, C, T extends PlatformPlugin<C>> {
}
HikariConfig databaseConfig = new HikariConfig();
databaseConfig.setDriverClassName(driver);
String database = config.getString("database");
databaseConfig.setConnectionTimeout(config.getInt("timeout", 30) * 1_000L);

View File

@ -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);

View File

@ -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);
}

View File

@ -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");