Use the non-registering MySQL driver and revert to using DataSource for MariaDB

This commit is contained in:
Luck 2020-11-14 13:44:32 +00:00
parent 129a10aa60
commit 9af6dccd9d
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
3 changed files with 35 additions and 4 deletions

View File

@ -89,6 +89,18 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
properties.putIfAbsent("socketTimeout", String.valueOf(TimeUnit.SECONDS.toMillis(30)));
}
/**
* Sets the given connection properties onto the config.
*
* @param config the hikari config
* @param properties the properties
*/
protected void setProperties(HikariConfig config, Map<String, String> properties) {
for (Map.Entry<String, String> property : properties.entrySet()) {
config.addDataSourceProperty(property.getKey(), property.getValue());
}
}
@Override
public void init(LuckPermsPlugin plugin) {
HikariConfig config;
@ -119,9 +131,7 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
overrideProperties(properties);
// set the properties
for (Map.Entry<String, String> property : properties.entrySet()) {
config.addDataSourceProperty(property.getKey(), property.getValue());
}
setProperties(config, properties);
// configure the connection pool
config.setMaximumPoolSize(this.configuration.getMaxPoolSize());

View File

@ -29,7 +29,9 @@ import com.zaxxer.hikari.HikariConfig;
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
public class MariaDbConnectionFactory extends HikariConnectionFactory {
public MariaDbConnectionFactory(StorageCredentials configuration) {
@ -48,12 +50,30 @@ public class MariaDbConnectionFactory extends HikariConnectionFactory {
@Override
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
config.setDataSourceClassName("org.mariadb.jdbc.MariaDbDataSource");
config.addDataSourceProperty("serverName", address);
config.addDataSourceProperty("port", port);
config.addDataSourceProperty("databaseName", databaseName);
config.setUsername(username);
config.setPassword(password);
config.setDriverClassName("org.mariadb.jdbc.Driver");
config.setJdbcUrl("jdbc:mariadb://" + address + ":" + port + "/" + databaseName);
config.setUsername(username);
config.setPassword(password);
}
@Override
protected void setProperties(HikariConfig config, Map<String, String> properties) {
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.
config.addDataSourceProperty("properties", propertiesString);
}
@Override
public Function<String, String> getStatementProcessor() {
return s -> s.replace("'", "`"); // use backticks for quotes

View File

@ -49,7 +49,8 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
@Override
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
// other plugins shouldn't be able to use our driver, so use the non-registering type.
config.setDriverClassName("com.mysql.cj.jdbc.NonRegisteringDriver");
config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + databaseName);
config.setUsername(username);
config.setPassword(password);