From 526448ce405138ea2209dd4a6c0f175e0e5f0795 Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 4 Feb 2019 19:36:53 +0000 Subject: [PATCH] Don't try to set unicode connection properties for PostgreSQL (#1134) --- .../hikari/HikariConnectionFactory.java | 7 +++--- .../hikari/MariaDbConnectionFactory.java | 6 ++--- .../hikari/MySqlConnectionFactory.java | 23 ++++++++++--------- .../hikari/PostgreConnectionFactory.java | 10 ++++++++ 4 files changed, 28 insertions(+), 18 deletions(-) diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java index 8336e8ea3..4db3f4367 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/HikariConnectionFactory.java @@ -34,6 +34,7 @@ import me.lucko.luckperms.common.storage.misc.StorageCredentials; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; @@ -50,8 +51,8 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { return null; } - protected void appendProperties(HikariConfig config, StorageCredentials credentials) { - for (Map.Entry property : credentials.getProperties().entrySet()) { + protected void appendProperties(HikariConfig config, Map properties) { + for (Map.Entry property : properties.entrySet()) { config.addDataSourceProperty(property.getKey(), property.getValue()); } } @@ -76,7 +77,7 @@ public abstract class HikariConnectionFactory implements ConnectionFactory { config.setPoolName("luckperms-hikari"); appendConfigurationInfo(config); - appendProperties(config, this.configuration); + appendProperties(config, new HashMap<>(this.configuration.getProperties())); config.setMaximumPoolSize(this.configuration.getMaxPoolSize()); config.setMinimumIdle(this.configuration.getMinIdleConnections()); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MariaDbConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MariaDbConnectionFactory.java index 10382fbcc..762b33485 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MariaDbConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MariaDbConnectionFactory.java @@ -30,7 +30,6 @@ import com.zaxxer.hikari.HikariConfig; import me.lucko.luckperms.common.storage.misc.StorageCredentials; import java.util.Map; -import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; @@ -50,13 +49,12 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory { } @Override - protected void appendProperties(HikariConfig config, StorageCredentials credentials) { - Set> properties = credentials.getProperties().entrySet(); + protected void appendProperties(HikariConfig config, Map properties) { if (properties.isEmpty()) { return; } - String propertiesString = properties.stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(";")); + String propertiesString = properties.entrySet().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. diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MySqlConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MySqlConnectionFactory.java index a982d75ba..994694352 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MySqlConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/MySqlConnectionFactory.java @@ -29,6 +29,7 @@ import com.zaxxer.hikari.HikariConfig; import me.lucko.luckperms.common.storage.misc.StorageCredentials; +import java.util.Map; import java.util.function.Function; public class MySqlConnectionFactory extends HikariConnectionFactory { @@ -47,20 +48,20 @@ public class MySqlConnectionFactory extends HikariConnectionFactory { } @Override - protected void appendProperties(HikariConfig config, StorageCredentials credentials) { - config.addDataSourceProperty("cachePrepStmts", "true"); - config.addDataSourceProperty("alwaysSendSetIsolation", "false"); - config.addDataSourceProperty("cacheServerConfiguration", "true"); - config.addDataSourceProperty("elideSetAutoCommits", "true"); - config.addDataSourceProperty("useLocalSessionState", "true"); + protected void appendProperties(HikariConfig config, Map properties) { + properties.putIfAbsent("cachePrepStmts", "true"); + properties.putIfAbsent("alwaysSendSetIsolation", "false"); + properties.putIfAbsent("cacheServerConfiguration", "true"); + properties.putIfAbsent("elideSetAutoCommits", "true"); + properties.putIfAbsent("useLocalSessionState", "true"); - config.addDataSourceProperty("useServerPrepStmts", "true"); - config.addDataSourceProperty("prepStmtCacheSize", "250"); - config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); - config.addDataSourceProperty("cacheCallableStmts", "true"); + properties.putIfAbsent("useServerPrepStmts", "true"); + properties.putIfAbsent("prepStmtCacheSize", "250"); + properties.putIfAbsent("prepStmtCacheSqlLimit", "2048"); + properties.putIfAbsent("cacheCallableStmts", "true"); // append configurable properties - super.appendProperties(config, credentials); + super.appendProperties(config, properties); } @Override diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgreConnectionFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgreConnectionFactory.java index def31513f..16d6957ce 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgreConnectionFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/implementation/sql/connection/hikari/PostgreConnectionFactory.java @@ -29,6 +29,7 @@ import com.zaxxer.hikari.HikariConfig; import me.lucko.luckperms.common.storage.misc.StorageCredentials; +import java.util.Map; import java.util.function.Function; public class PostgreConnectionFactory extends HikariConnectionFactory { @@ -41,6 +42,15 @@ public class PostgreConnectionFactory extends HikariConnectionFactory { return "PostgreSQL"; } + @Override + protected void appendProperties(HikariConfig config, Map properties) { + // remove the default config properties which don't exist for PostgreSQL + properties.remove("useUnicode"); + properties.remove("characterEncoding"); + + super.appendProperties(config, properties); + } + @Override protected void appendConfigurationInfo(HikariConfig config) { String address = this.configuration.getAddress();