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

View File

@ -22,6 +22,7 @@
package me.lucko.luckperms.storage.methods;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import lombok.Cleanup;
import me.lucko.luckperms.LuckPermsPlugin;
@ -50,20 +51,32 @@ public class MySQLDatastore extends SQLDatastore {
@Override
public void init() {
hikari = new HikariDataSource();
HikariConfig config = new HikariConfig();
final String address = configuration.getAddress();
final String database = configuration.getDatabase();
final String username = configuration.getUsername();
final String password = configuration.getPassword();
hikari.setMaximumPoolSize(10);
hikari.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
hikari.addDataSourceProperty("serverName", address.split(":")[0]);
hikari.addDataSourceProperty("port", address.split(":")[1]);
hikari.addDataSourceProperty("databaseName", database);
hikari.addDataSourceProperty("user", username);
hikari.addDataSourceProperty("password", password);
config.setMaximumPoolSize(10);
config.setPoolName("luckperms");
config.setDataSourceClassName("com.mysql.jdbc.jdbc2.optional.MysqlDataSource");
config.addDataSourceProperty("serverName", address.split(":")[0]);
config.addDataSourceProperty("port", address.split(":")[1]);
config.addDataSourceProperty("databaseName", database);
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)) {
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 Connection connection = null;
private final Object connectionLock = new Object();
public SQLiteDatastore(LuckPermsPlugin plugin, File file) {
super(plugin, "SQLite");
@ -107,12 +108,14 @@ public class SQLiteDatastore extends SQLDatastore {
@Override
Connection getConnection() throws SQLException {
if (connection == null || connection.isClosed()) {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ignored) {}
synchronized (connectionLock) {
if (connection == null || connection.isClosed()) {
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException ignored) {}
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
connection = DriverManager.getConnection("jdbc:sqlite:" + file.getAbsolutePath());
}
}
return connection;