From 717835e5c15f728048555d0c534ccef5ffb72302 Mon Sep 17 00:00:00 2001 From: Luck Date: Sat, 25 Nov 2017 11:35:05 +0000 Subject: [PATCH] Add config option for defining extra SQL connection properties (#563) --- bukkit/src/main/resources/config.yml | 5 +++++ bungee/src/main/resources/config.yml | 5 +++++ .../lucko/luckperms/common/config/ConfigKeys.java | 4 +++- .../common/storage/StorageCredentials.java | 3 +++ .../common/storage/dao/SplitStorageDao.java | 7 ++----- .../hikari/HikariConnectionFactory.java | 8 +++++--- .../hikari/MariaDbConnectionFactory.java | 15 +++++++++++++-- .../connection/hikari/MySqlConnectionFactory.java | 7 +++---- .../hikari/PostgreConnectionFactory.java | 1 - sponge/src/main/resources/luckperms.conf | 6 ++++++ 10 files changed, 45 insertions(+), 16 deletions(-) diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index d32b495a..2a6885e3 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -325,6 +325,11 @@ data: # connection from the pool, before timing out. connection-timeout: 15000 # 15 seconds + # This setting allows you to define extra properties for connections. + properties: + useUnicode: true + characterEncoding: utf8 + # The prefix for all LuckPerms tables. Change this is you want to use different tables for # different servers. # diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index 1d242a4f..c42b50cf 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -271,6 +271,11 @@ data: # connection from the pool, before timing out. connection-timeout: 15000 # 15 seconds + # This setting allows you to define extra properties for connections. + properties: + useUnicode: true + characterEncoding: utf8 + # The prefix for all LuckPerms tables. Change this is you want to use different tables for # different servers. # diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index 7196087f..4a723e1c 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -368,12 +368,14 @@ public class ConfigKeys { int minIdle = c.getInt("data.pool-settings.minimum-idle", maxPoolSize); int maxLifetime = c.getInt("data.pool-settings.maximum-lifetime", 1800000); int connectionTimeout = c.getInt("data.pool-settings.connection-timeout", 15000); + Map props = ImmutableMap.copyOf(c.getMap("data.pool-settings.properties", ImmutableMap.of("useUnicode", "true", "characterEncoding", "utf8"))); + return new StorageCredentials( c.getString("data.address", null), c.getString("data.database", null), c.getString("data.username", null), c.getString("data.password", null), - maxPoolSize, minIdle, maxLifetime, connectionTimeout + maxPoolSize, minIdle, maxLifetime, connectionTimeout, props ); })); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageCredentials.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageCredentials.java index 81c2d433..2fc5be46 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageCredentials.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageCredentials.java @@ -28,6 +28,8 @@ package me.lucko.luckperms.common.storage; import lombok.AllArgsConstructor; import lombok.Getter; +import java.util.Map; + @Getter @AllArgsConstructor public class StorageCredentials { @@ -40,5 +42,6 @@ public class StorageCredentials { private int minIdleConnections; private int maxLifetime; private int connectionTimeout; + private Map properties; } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/SplitStorageDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/SplitStorageDao.java index 2ff0fb5f..40f6d88b 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/SplitStorageDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/SplitStorageDao.java @@ -55,13 +55,10 @@ public class SplitStorageDao extends AbstractDao { @Override public void init() { boolean success = true; - backing.values().forEach(AbstractDao::init); for (AbstractDao ds : backing.values()) { - if (!ds.isAcceptingLogins()) { - success = false; - } + ds.init(); + success = success && ds.isAcceptingLogins(); } - setAcceptingLogins(success); } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java index 41cae0e3..9545c783 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/HikariConnectionFactory.java @@ -52,8 +52,10 @@ public abstract class HikariConnectionFactory extends AbstractConnectionFactory return null; } - protected void appendProperties(HikariConfig config) { - + protected void appendProperties(HikariConfig config, StorageCredentials credentials) { + for (Map.Entry property : credentials.getProperties().entrySet()) { + config.addDataSourceProperty(property.getKey(), property.getValue()); + } } protected void appendConfigurationInfo(HikariConfig config) { @@ -76,7 +78,7 @@ public abstract class HikariConnectionFactory extends AbstractConnectionFactory config.setPoolName("luckperms"); appendConfigurationInfo(config); - appendProperties(config); + appendProperties(config, configuration); config.setMaximumPoolSize(configuration.getMaxPoolSize()); config.setMinimumIdle(configuration.getMinIdleConnections()); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java index 609c73fc..37f7d705 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MariaDbConnectionFactory.java @@ -29,6 +29,10 @@ import com.zaxxer.hikari.HikariConfig; import me.lucko.luckperms.common.storage.StorageCredentials; +import java.util.Map; +import java.util.Set; +import java.util.stream.Collectors; + public class MariaDbConnectionFactory extends HikariConnectionFactory { public MariaDbConnectionFactory(StorageCredentials configuration) { super("MariaDB", configuration); @@ -40,10 +44,17 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory { } @Override - protected void appendProperties(HikariConfig config) { + protected void appendProperties(HikariConfig config, StorageCredentials credentials) { + Set> properties = credentials.getProperties().entrySet(); + if (properties.isEmpty()) { + return; + } + + String propertiesString = properties.stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(";")); + // kinda hacky. this will call #setProperties on the datasource, which will append these options // onto the connections. - config.addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8"); + config.addDataSourceProperty("properties", propertiesString); } private static boolean classExists(String clazz) { diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java index 8e2f5517..df6f65e8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/MySqlConnectionFactory.java @@ -48,7 +48,7 @@ public class MySqlConnectionFactory extends HikariConnectionFactory { } @Override - protected void appendProperties(HikariConfig config) { + protected void appendProperties(HikariConfig config, StorageCredentials credentials) { config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("alwaysSendSetIsolation", "false"); config.addDataSourceProperty("cacheServerConfiguration", "true"); @@ -60,9 +60,8 @@ public class MySqlConnectionFactory extends HikariConnectionFactory { config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.addDataSourceProperty("cacheCallableStmts", "true"); - // make sure unicode characters can be used. - config.addDataSourceProperty("characterEncoding", "utf8"); - config.addDataSourceProperty("useUnicode", "true"); + // append configurable properties + super.appendProperties(config, credentials); } } diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/PostgreConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/PostgreConnectionFactory.java index 968cdbd2..570fb04e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/PostgreConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/sql/connection/hikari/PostgreConnectionFactory.java @@ -52,5 +52,4 @@ public class PostgreConnectionFactory extends HikariConnectionFactory { config.addDataSourceProperty("user", username); config.addDataSourceProperty("password", password); } - } diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index 6efe4afd..f2780943 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -274,6 +274,12 @@ data { # This setting controls the maximum number of milliseconds that the plugin will wait for a # connection from the pool, before timing out. connection-timeout=15000 # 15 seconds + + # This setting allows you to define extra properties for connections. + properties { + useUnicode=true + characterEncoding="utf8" + } } # The prefix for all LuckPerms tables. Change this is you want to use different tables for