Unregister mysql driver after use

Affects issues:
- Fixed #1804
This commit is contained in:
Risto Lahtela 2021-03-19 17:45:52 +02:00
parent b956a8f2ba
commit a71d2f730f
2 changed files with 20 additions and 11 deletions

View File

@ -64,17 +64,6 @@ public class DependencyStartup {
); );
logger.info("Loading runtime dependencies.."); logger.info("Loading runtime dependencies..");
dependencyLoader.load(); dependencyLoader.load();
try {
Class.forName("org.sqlite.JDBC");
} catch (ClassNotFoundException e) {
logger.error("Could not load SQLite driver");
}
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
logger.error("Could not load MySQL driver");
}
} }
} }

View File

@ -35,7 +35,10 @@ import net.playeranalytics.plugin.server.PluginLogger;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
import java.sql.Connection; import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -123,6 +126,23 @@ public class MySQLDB extends SQLDB {
this.dataSource = new HikariDataSource(hikariConfig); this.dataSource = new HikariDataSource(hikariConfig);
} catch (HikariPool.PoolInitializationException e) { } catch (HikariPool.PoolInitializationException e) {
throw new DBInitException("Failed to set-up HikariCP Datasource: " + e.getMessage(), e); throw new DBInitException("Failed to set-up HikariCP Datasource: " + e.getMessage(), e);
} finally {
unloadMySQLDriver();
}
}
private void unloadMySQLDriver() {
// Avoid issues with other plugins by removing the mysql driver from driver manager
Enumeration<Driver> drivers = DriverManager.getDrivers();
while (drivers.hasMoreElements()) {
Driver driver = drivers.nextElement();
if ("com.mysql.cj.jdbc.Driver".equals(driver.getClass().getName())) {
try {
DriverManager.deregisterDriver(driver);
} catch (SQLException e) {
// ignore
}
}
} }
} }