mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2025-01-30 12:11:49 +01:00
Use a better approach for avoiding MySQL driver sharing
This commit is contained in:
parent
23c7eb79d0
commit
c39749e526
@ -101,6 +101,13 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called after the Hikari pool has been initialised
|
||||
*/
|
||||
protected void postInitialize() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(LuckPermsPlugin plugin) {
|
||||
HikariConfig config;
|
||||
@ -144,6 +151,8 @@ public abstract class HikariConnectionFactory implements ConnectionFactory {
|
||||
config.setInitializationFailTimeout(-1);
|
||||
|
||||
this.hikari = new HikariDataSource(config);
|
||||
|
||||
postInitialize();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -29,6 +29,10 @@ import com.zaxxer.hikari.HikariConfig;
|
||||
|
||||
import me.lucko.luckperms.common.storage.misc.StorageCredentials;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Enumeration;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
|
||||
@ -49,13 +53,32 @@ public class MySqlConnectionFactory extends HikariConnectionFactory {
|
||||
|
||||
@Override
|
||||
protected void configureDatabase(HikariConfig config, String address, String port, String databaseName, String username, String password) {
|
||||
// other plugins shouldn't be able to use our driver, so use the non-registering type.
|
||||
config.setDriverClassName("com.mysql.cj.jdbc.NonRegisteringDriver");
|
||||
config.setDriverClassName("com.mysql.cj.jdbc.Driver");
|
||||
config.setJdbcUrl("jdbc:mysql://" + address + ":" + port + "/" + databaseName);
|
||||
config.setUsername(username);
|
||||
config.setPassword(password);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void postInitialize() {
|
||||
super.postInitialize();
|
||||
|
||||
// Calling Class.forName("com.mysql.cj.jdbc.Driver") is enough to call the static initializer
|
||||
// which makes our driver available in DriverManager. We don't want that, so unregister it after
|
||||
// the pool has been setup.
|
||||
Enumeration<Driver> drivers = DriverManager.getDrivers();
|
||||
while (drivers.hasMoreElements()) {
|
||||
Driver driver = drivers.nextElement();
|
||||
if (driver.getClass().getName().equals("com.mysql.cj.jdbc.Driver")) {
|
||||
try {
|
||||
DriverManager.deregisterDriver(driver);
|
||||
} catch (SQLException e) {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void overrideProperties(Map<String, String> properties) {
|
||||
// https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration
|
||||
|
Loading…
Reference in New Issue
Block a user