Fix possible race condition & maybe use better Hikari flags

This commit is contained in:
Luck 2016-09-18 23:13:20 +01:00
parent 66818e4c82
commit bf247b71e1
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 37 additions and 18 deletions

View File

@ -38,6 +38,7 @@ public class H2Datastore extends SQLDatastore {
private final File file; private final File file;
private Connection connection = null; private Connection connection = null;
private final Object connectionLock = new Object();
public H2Datastore(LuckPermsPlugin plugin, File file) { public H2Datastore(LuckPermsPlugin plugin, File file) {
super(plugin, "H2"); super(plugin, "H2");
@ -107,12 +108,14 @@ public class H2Datastore extends SQLDatastore {
@Override @Override
Connection getConnection() throws SQLException { Connection getConnection() throws SQLException {
if (connection == null || connection.isClosed()) { synchronized (connectionLock) {
try { if (connection == null || connection.isClosed()) {
Class.forName("org.h2.Driver"); try {
} catch (ClassNotFoundException ignored) {} Class.forName("org.h2.Driver");
} catch (ClassNotFoundException ignored) {}
connection = DriverManager.getConnection("jdbc:h2:" + file.getAbsolutePath()); connection = DriverManager.getConnection("jdbc:h2:" + file.getAbsolutePath());
}
} }
return connection; return connection;

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.storage.methods; package me.lucko.luckperms.storage.methods;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
import lombok.Cleanup; import lombok.Cleanup;
import me.lucko.luckperms.LuckPermsPlugin; import me.lucko.luckperms.LuckPermsPlugin;
@ -50,20 +51,32 @@ public class MySQLDatastore extends SQLDatastore {
@Override @Override
public void init() { public void init() {
hikari = new HikariDataSource(); HikariConfig config = new HikariConfig();
final String address = configuration.getAddress(); final String address = configuration.getAddress();
final String database = configuration.getDatabase(); final String database = configuration.getDatabase();
final String username = configuration.getUsername(); final String username = configuration.getUsername();
final String password = configuration.getPassword(); final String password = configuration.getPassword();
hikari.setMaximumPoolSize(10); config.setMaximumPoolSize(10);
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource"); config.setPoolName("luckperms");
hikari.addDataSourceProperty("serverName", address.split(":")[0]); config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hikari.addDataSourceProperty("port", address.split(":")[1]); config.addDataSourceProperty("serverName", address.split(":")[0]);
hikari.addDataSourceProperty("databaseName", database); config.addDataSourceProperty("port", address.split(":")[1]);
hikari.addDataSourceProperty("user", username); config.addDataSourceProperty("databaseName", database);
hikari.addDataSourceProperty("password", password); config.addDataSourceProperty("user", username);
config.addDataSourceProperty("password", password);
config.addDataSourceProperty("cachePrepStmts", true);
config.addDataSourceProperty("prepStmtCacheSize", 250);
config.addDataSourceProperty("prepStmtCacheSqlLimit", 2048);
config.addDataSourceProperty("useServerPrepStmts", true);
config.addDataSourceProperty("cacheCallableStmts", true);
config.addDataSourceProperty("alwaysSendSetIsolation", false);
config.addDataSourceProperty("cacheServerConfiguration", true);
config.addDataSourceProperty("elideSetAutoCommits", true);
config.addDataSourceProperty("useLocalSessionState", true);
hikari = new HikariDataSource(config);
if (!setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS, CREATETABLE_TRACKS, CREATETABLE_ACTION)) { if (!setupTables(CREATETABLE_UUID, CREATETABLE_USERS, CREATETABLE_GROUPS, CREATETABLE_TRACKS, CREATETABLE_ACTION)) {
plugin.getLog().severe("Error occurred whilst initialising the database."); plugin.getLog().severe("Error occurred whilst initialising the database.");

View File

@ -38,6 +38,7 @@ public class SQLiteDatastore extends SQLDatastore {
private final File file; private final File file;
private Connection connection = null; private Connection connection = null;
private final Object connectionLock = new Object();
public SQLiteDatastore(LuckPermsPlugin plugin, File file) { public SQLiteDatastore(LuckPermsPlugin plugin, File file) {
super(plugin, "SQLite"); super(plugin, "SQLite");
@ -107,12 +108,14 @@ public class SQLiteDatastore extends SQLDatastore {
@Override @Override
Connection getConnection() throws SQLException { Connection getConnection() throws SQLException {
if (connection == null || connection.isClosed()) { synchronized (connectionLock) {
try { if (connection == null || connection.isClosed()) {
Class.forName("org.sqlite.JDBC"); try {
} catch (ClassNotFoundException ignored) {} Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ignored) {}
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath()); connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
}
} }
return connection; return connection;