Skip to content

Commit 6beaf19

Browse files
author
games647
committed
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
1 parent 62d7320 commit 6beaf19

File tree

4 files changed

+27
-23
lines changed

4 files changed

+27
-23
lines changed

core/src/main/java/com/github/games647/fastlogin/core/shared/FastLoginCore.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,6 @@ public boolean setupDatabase() {
230230
}
231231

232232
HikariConfig databaseConfig = new HikariConfig();
233-
databaseConfig.setDriverClassName(driver);
234-
235233
String database = config.getString("database");
236234

237235
databaseConfig.setConnectionTimeout(config.getInt("timeout", 30) * 1_000L);

core/src/main/java/com/github/games647/fastlogin/core/storage/MySQLStorage.java

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,18 @@
3030

3131
public class MySQLStorage extends SQLStorage {
3232

33+
private static final String JDBC_PROTOCOL = "jdbc:";
34+
3335
public MySQLStorage(FastLoginCore<?, ?, ?> core, String driver, String host, int port, String database,
3436
HikariConfig config, boolean useSSL) {
35-
super(core,
36-
buildJDBCUrl(driver, host, port, database),
37-
setParams(config, useSSL));
37+
super(core, setParams(config, driver, host, port, database, useSSL));
3838
}
3939

40-
private static String buildJDBCUrl(String driver, String host, int port, String database) {
41-
String protocol = "mysql";
42-
if (driver.contains("mariadb")) {
43-
protocol = "mariadb";
44-
}
45-
46-
return protocol + "://" + host + ':' + port + '/' + database;
47-
}
40+
private static HikariConfig setParams(HikariConfig config,
41+
String driver, String host, int port, String database,
42+
boolean useSSL) {
43+
config.setDriverClassName(driver);
4844

49-
private static HikariConfig setParams(HikariConfig config, boolean useSSL) {
5045
// Require SSL on the server if requested in config - this will also verify certificate
5146
// Those values are deprecated in favor of sslMode
5247
config.addDataSourceProperty("useSSL", useSSL);
@@ -56,11 +51,22 @@ private static HikariConfig setParams(HikariConfig config, boolean useSSL) {
5651
// could be useful for hiding server details
5752
config.addDataSourceProperty("paranoid", true);
5853

54+
config.setJdbcUrl(JDBC_PROTOCOL + buildJDBCUrl(driver, host, port, database));
55+
5956
// enable MySQL specific optimizations
6057
addPerformanceProperties(config);
6158
return config;
6259
}
6360

61+
private static String buildJDBCUrl(String driver, String host, int port, String database) {
62+
String protocol = "mysql";
63+
if (driver.contains("mariadb")) {
64+
protocol = "mariadb";
65+
}
66+
67+
return protocol + "://" + host + ':' + port + '/' + database;
68+
}
69+
6470
private static void addPerformanceProperties(HikariConfig config) {
6571
// disabled by default - will return the same prepared statement instance
6672
config.addDataSourceProperty("cachePrepStmts", true);

core/src/main/java/com/github/games647/fastlogin/core/storage/SQLStorage.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545

4646
public abstract class SQLStorage implements AuthStorage {
4747

48-
private static final String JDBC_PROTOCOL = "jdbc:";
49-
5048
protected static final String PREMIUM_TABLE = "premium";
5149
protected static final String CREATE_TABLE_STMT = "CREATE TABLE IF NOT EXISTS `" + PREMIUM_TABLE + "` ("
5250
+ "`UserID` INTEGER PRIMARY KEY AUTO_INCREMENT, "
@@ -70,7 +68,7 @@ public abstract class SQLStorage implements AuthStorage {
7068
protected final FastLoginCore<?, ?, ?> core;
7169
protected final HikariDataSource dataSource;
7270

73-
public SQLStorage(FastLoginCore<?, ?, ?> core, String jdbcURL, HikariConfig config) {
71+
public SQLStorage(FastLoginCore<?, ?, ?> core, HikariConfig config) {
7472
this.core = core;
7573
config.setPoolName(core.getPlugin().getName());
7674

@@ -79,7 +77,6 @@ public SQLStorage(FastLoginCore<?, ?, ?> core, String jdbcURL, HikariConfig conf
7977
config.setThreadFactory(platformThreadFactory);
8078
}
8179

82-
config.setJdbcUrl(JDBC_PROTOCOL + jdbcURL);
8380
this.dataSource = new HikariDataSource(config);
8481
}
8582

core/src/main/java/com/github/games647/fastlogin/core/storage/SQLiteStorage.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,22 @@
3939

4040
public class SQLiteStorage extends SQLStorage {
4141

42+
private static final String SQLITE_DRIVER = "org.sqlite.SQLiteDataSource";
4243
private final Lock lock = new ReentrantLock();
4344

4445
public SQLiteStorage(FastLoginCore<?, ?, ?> core, String databasePath, HikariConfig config) {
45-
super(core,
46-
"sqlite://" + replacePathVariables(core.getPlugin(), databasePath),
47-
setParams(config));
46+
super(core, setParams(config, replacePathVariables(core.getPlugin(), databasePath)));
4847
}
4948

50-
private static HikariConfig setParams(HikariConfig config) {
49+
private static HikariConfig setParams(HikariConfig config, String path) {
50+
config.setDataSourceClassName(SQLITE_DRIVER);
51+
5152
config.setConnectionTestQuery("SELECT 1");
5253
config.setMaximumPoolSize(1);
5354

54-
//a try to fix https://www.spigotmc.org/threads/fastlogin.101192/page-26#post-1874647
55+
config.addDataSourceProperty("databaseName", path);
56+
57+
// a try to fix https://www.spigotmc.org/threads/fastlogin.101192/page-26#post-1874647
5558
// format strings retrieved by the timestamp column to match them from MySQL
5659
config.addDataSourceProperty("date_string_format", "yyyy-MM-dd HH:mm:ss");
5760

0 commit comments

Comments
 (0)