Add config option for defining extra SQL connection properties (#563)
This commit is contained in:
parent
e9ba62dbd9
commit
717835e5c1
@ -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.
|
||||
#
|
||||
|
@ -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.
|
||||
#
|
||||
|
@ -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<String, String> 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
|
||||
);
|
||||
}));
|
||||
|
||||
|
@ -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<String, String> properties;
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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<String, String> 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());
|
||||
|
@ -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<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
|
||||
// onto the connections.
|
||||
config.addDataSourceProperty("properties", "useUnicode=true;characterEncoding=utf8");
|
||||
config.addDataSourceProperty("properties", propertiesString);
|
||||
}
|
||||
|
||||
private static boolean classExists(String clazz) {
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -52,5 +52,4 @@ public class PostgreConnectionFactory extends HikariConnectionFactory {
|
||||
config.addDataSourceProperty("user", username);
|
||||
config.addDataSourceProperty("password", password);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user