Add config option for defining extra SQL connection properties (#563)

This commit is contained in:
Luck 2017-11-25 11:35:05 +00:00
parent e9ba62dbd9
commit 717835e5c1
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
10 changed files with 45 additions and 16 deletions

View File

@ -325,6 +325,11 @@ data:
# connection from the pool, before timing out. # connection from the pool, before timing out.
connection-timeout: 15000 # 15 seconds 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 # The prefix for all LuckPerms tables. Change this is you want to use different tables for
# different servers. # different servers.
# #

View File

@ -271,6 +271,11 @@ data:
# connection from the pool, before timing out. # connection from the pool, before timing out.
connection-timeout: 15000 # 15 seconds 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 # The prefix for all LuckPerms tables. Change this is you want to use different tables for
# different servers. # different servers.
# #

View File

@ -368,12 +368,14 @@ public class ConfigKeys {
int minIdle = c.getInt("data.pool-settings.minimum-idle", maxPoolSize); int minIdle = c.getInt("data.pool-settings.minimum-idle", maxPoolSize);
int maxLifetime = c.getInt("data.pool-settings.maximum-lifetime", 1800000); int maxLifetime = c.getInt("data.pool-settings.maximum-lifetime", 1800000);
int connectionTimeout = c.getInt("data.pool-settings.connection-timeout", 15000); int connectionTimeout = c.getInt("data.pool-settings.connection-timeout", 15000);
Map<String, String> props = ImmutableMap.copyOf(c.getMap("data.pool-settings.properties", ImmutableMap.of("useUnicode", "true", "characterEncoding", "utf8")));
return new StorageCredentials( return new StorageCredentials(
c.getString("data.address", null), c.getString("data.address", null),
c.getString("data.database", null), c.getString("data.database", null),
c.getString("data.username", null), c.getString("data.username", null),
c.getString("data.password", null), c.getString("data.password", null),
maxPoolSize, minIdle, maxLifetime, connectionTimeout maxPoolSize, minIdle, maxLifetime, connectionTimeout, props
); );
})); }));

View File

@ -28,6 +28,8 @@ package me.lucko.luckperms.common.storage;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import java.util.Map;
@Getter @Getter
@AllArgsConstructor @AllArgsConstructor
public class StorageCredentials { public class StorageCredentials {
@ -40,5 +42,6 @@ public class StorageCredentials {
private int minIdleConnections; private int minIdleConnections;
private int maxLifetime; private int maxLifetime;
private int connectionTimeout; private int connectionTimeout;
private Map<String, String> properties;
} }

View File

@ -55,13 +55,10 @@ public class SplitStorageDao extends AbstractDao {
@Override @Override
public void init() { public void init() {
boolean success = true; boolean success = true;
backing.values().forEach(AbstractDao::init);
for (AbstractDao ds : backing.values()) { for (AbstractDao ds : backing.values()) {
if (!ds.isAcceptingLogins()) { ds.init();
success = false; success = success && ds.isAcceptingLogins();
}
} }
setAcceptingLogins(success); setAcceptingLogins(success);
} }

View File

@ -52,8 +52,10 @@ public abstract class HikariConnectionFactory extends AbstractConnectionFactory
return null; return null;
} }
protected void appendProperties(HikariConfig config) { protected void appendProperties(HikariConfig config, StorageCredentials credentials) {
for (Map.Entry<String, String> property : credentials.getProperties().entrySet()) {
config.addDataSourceProperty(property.getKey(), property.getValue());
}
} }
protected void appendConfigurationInfo(HikariConfig config) { protected void appendConfigurationInfo(HikariConfig config) {
@ -76,7 +78,7 @@ public abstract class HikariConnectionFactory extends AbstractConnectionFactory
config.setPoolName("luckperms"); config.setPoolName("luckperms");
appendConfigurationInfo(config); appendConfigurationInfo(config);
appendProperties(config); appendProperties(config, configuration);
config.setMaximumPoolSize(configuration.getMaxPoolSize()); config.setMaximumPoolSize(configuration.getMaxPoolSize());
config.setMinimumIdle(configuration.getMinIdleConnections()); config.setMinimumIdle(configuration.getMinIdleConnections());

View File

@ -29,6 +29,10 @@ import com.zaxxer.hikari.HikariConfig;
import me.lucko.luckperms.common.storage.StorageCredentials; 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 class MariaDbConnectionFactory extends HikariConnectionFactory {
public MariaDbConnectionFactory(StorageCredentials configuration) { public MariaDbConnectionFactory(StorageCredentials configuration) {
super("MariaDB", configuration); super("MariaDB", configuration);
@ -40,10 +44,17 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
} }
@Override @Override
protected void appendProperties(HikariConfig config) { protected void appendProperties(HikariConfig config, StorageCredentials credentials) {
Set<Map.Entry<String, String>> 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 // kinda hacky. this will call #setProperties on the datasource, which will append these options
// onto the connections. // onto the connections.
config.addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8"); config.addDataSourceProperty("properties", propertiesString);
} }
private static boolean classExists(String clazz) { private static boolean classExists(String clazz) {

View File

@ -48,7 +48,7 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
} }
@Override @Override
protected void appendProperties(HikariConfig config) { protected void appendProperties(HikariConfig config, StorageCredentials credentials) {
config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("alwaysSendSetIsolation", "false"); config.addDataSourceProperty("alwaysSendSetIsolation", "false");
config.addDataSourceProperty("cacheServerConfiguration", "true"); config.addDataSourceProperty("cacheServerConfiguration", "true");
@ -60,9 +60,8 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
config.addDataSourceProperty("cacheCallableStmts", "true"); config.addDataSourceProperty("cacheCallableStmts", "true");
// make sure unicode characters can be used. // append configurable properties
config.addDataSourceProperty("characterEncoding", "utf8"); super.appendProperties(config, credentials);
config.addDataSourceProperty("useUnicode", "true");
} }
} }

View File

@ -52,5 +52,4 @@ public class PostgreConnectionFactory extends HikariConnectionFactory {
config.addDataSourceProperty("user", username); config.addDataSourceProperty("user", username);
config.addDataSourceProperty("password", password); config.addDataSourceProperty("password", password);
} }
} }

View File

@ -274,6 +274,12 @@ data {
# This setting controls the maximum number of milliseconds that the plugin will wait for a # This setting controls the maximum number of milliseconds that the plugin will wait for a
# connection from the pool, before timing out. # connection from the pool, before timing out.
connection-timeout=15000 # 15 seconds 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 # The prefix for all LuckPerms tables. Change this is you want to use different tables for