mirror of
https://github.com/darksaid98/HuskHomes.git
synced 2026-12-03 06:28:49 +00:00
Migrate to HikariCP rather than raw drivers, close #40
This commit is contained in:
1 parent
b026fbcd0a
commit
894b50ad0d
7 files changed
+127
-42
No files matched your search
@@ -21,6 +21,12 @@ public abstract class Database {
|
||||
|
||||
public abstract void backup();
|
||||
|
||||
public final int hikariMaximumPoolSize = HuskHomes.getSettings().getHikariMaximumPoolSize();
|
||||
public final int hikariMinimumIdle = HuskHomes.getSettings().getHikariMinimumIdle();
|
||||
public final long hikariMaximumLifetime = HuskHomes.getSettings().getHikariMaximumLifetime();
|
||||
public final long hikariKeepAliveTime = HuskHomes.getSettings().getHikariKeepAliveTime();
|
||||
public final long hikariConnectionTimeOut = HuskHomes.getSettings().getHikariConnectionTimeOut();
|
||||
|
||||
public void initialize() {
|
||||
Connection connection = getConnection();
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
package me.william278.huskhomes2.data.SQL;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.logging.Level;
|
||||
@@ -71,7 +71,7 @@ public class MySQL extends Database {
|
||||
final String password = HuskHomes.getSettings().getMySQLPassword();
|
||||
final String params = HuskHomes.getSettings().getMySQLParams();
|
||||
|
||||
private Connection connection;
|
||||
private HikariDataSource dataSource;
|
||||
|
||||
public MySQL(HuskHomes instance) {
|
||||
super(instance);
|
||||
@@ -79,34 +79,47 @@ public class MySQL extends Database {
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
Connection connection = null;
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
if (dataSource == null || dataSource.isClosed()) {
|
||||
try {
|
||||
synchronized (HuskHomes.getInstance()) {
|
||||
Class.forName("com.mysql.cj.jdbc.Driver");
|
||||
connection = (DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database + params, username, password));
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the mySQL database: ", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "The mySQL JBDC library is missing! Please download and place this in the /lib folder.");
|
||||
// Create new HikariCP data source
|
||||
final String jdbcUrl = "jdbc:mysql://" + host + ":" + port + "/" + database + params;
|
||||
HikariDataSource hikariDataSource = new HikariDataSource();
|
||||
hikariDataSource.setJdbcUrl(jdbcUrl);
|
||||
|
||||
hikariDataSource.setUsername(username);
|
||||
hikariDataSource.setPassword(password);
|
||||
|
||||
hikariDataSource.setMaximumPoolSize(hikariMaximumPoolSize);
|
||||
hikariDataSource.setMinimumIdle(hikariMinimumIdle);
|
||||
hikariDataSource.setMaxLifetime(hikariMaximumLifetime);
|
||||
hikariDataSource.setKeepaliveTime(hikariKeepAliveTime);
|
||||
hikariDataSource.setConnectionTimeout(hikariConnectionTimeOut);
|
||||
|
||||
this.dataSource = hikariDataSource;
|
||||
connection = dataSource.getConnection();
|
||||
} catch (SQLException e ) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred initialising a new connection via HikariCP", e);
|
||||
}
|
||||
} else {
|
||||
connection = dataSource.getConnection();
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
connection = getConnection();
|
||||
Connection connection = getConnection();
|
||||
try(Statement statement = connection.createStatement()) {
|
||||
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables: ", e);
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables on the MySQL database: ", e);
|
||||
}
|
||||
initialize();
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
package me.william278.huskhomes2.data.SQL;
|
||||
|
||||
import com.zaxxer.hikari.HikariDataSource;
|
||||
import me.william278.huskhomes2.HuskHomes;
|
||||
|
||||
import java.io.*;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardCopyOption;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.time.Instant;
|
||||
import java.time.ZoneId;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.time.format.FormatStyle;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@@ -74,50 +74,65 @@ public class SQLite extends Database {
|
||||
|
||||
private static final String DATABASE_NAME = "HuskHomesData";
|
||||
|
||||
private Connection connection;
|
||||
private HikariDataSource dataSource;
|
||||
|
||||
public SQLite(HuskHomes instance) {
|
||||
super(instance);
|
||||
}
|
||||
|
||||
private void createDatabaseFileIfNotExist() {
|
||||
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
|
||||
if (!databaseFile.exists()) {
|
||||
try {
|
||||
if (!databaseFile.createNewFile()) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + DATABASE_NAME + ".db (file already exists)");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Connection getConnection() {
|
||||
Connection connection = null;
|
||||
try {
|
||||
if (connection == null || connection.isClosed()) {
|
||||
File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
|
||||
if (!databaseFile.exists()) {
|
||||
try {
|
||||
if (!databaseFile.createNewFile()) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to write new file: " + DATABASE_NAME + ".db (file already exists)");
|
||||
}
|
||||
} catch (IOException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred writing a file: " + DATABASE_NAME + ".db (" + e.getCause() + ")");
|
||||
}
|
||||
}
|
||||
if (dataSource == null || dataSource.isClosed()) {
|
||||
createDatabaseFileIfNotExist();
|
||||
try {
|
||||
Class.forName("org.sqlite.JDBC");
|
||||
connection = (DriverManager.getConnection("jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db"));
|
||||
} catch (SQLException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An exception occurred initialising the SQLite database", ex);
|
||||
} catch (ClassNotFoundException ex) {
|
||||
plugin.getLogger().log(Level.SEVERE, "The SQLite JBDC library is missing! Please download and place this in the /lib folder.");
|
||||
final String jdbcUrl = "jdbc:sqlite:" + plugin.getDataFolder().getAbsolutePath() + "/" + DATABASE_NAME + ".db";
|
||||
HikariDataSource hikariDataSource = new HikariDataSource();
|
||||
hikariDataSource.setJdbcUrl(jdbcUrl);
|
||||
|
||||
hikariDataSource.setMaximumPoolSize(hikariMaximumPoolSize);
|
||||
hikariDataSource.setMinimumIdle(hikariMinimumIdle);
|
||||
hikariDataSource.setMaxLifetime(hikariMaximumLifetime);
|
||||
hikariDataSource.setKeepaliveTime(hikariKeepAliveTime);
|
||||
hikariDataSource.setConnectionTimeout(hikariConnectionTimeOut);
|
||||
|
||||
this.dataSource = hikariDataSource;
|
||||
connection = dataSource.getConnection();
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred initialising a new connection via HikariCP", e);
|
||||
}
|
||||
} else {
|
||||
connection = dataSource.getConnection();
|
||||
}
|
||||
} catch (SQLException exception) {
|
||||
plugin.getLogger().log(Level.WARNING, "An error occurred checking the status of the SQL connection: ", exception);
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An SQL exception occurred getting the mySQL connection via the existing Hikari pool", e);
|
||||
}
|
||||
return connection;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
connection = getConnection();
|
||||
Connection connection = getConnection();
|
||||
try (Statement statement = connection.createStatement()) {
|
||||
for (String tableCreationStatement : SQL_SETUP_STATEMENTS) {
|
||||
statement.execute(tableCreationStatement);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables: ", e);
|
||||
plugin.getLogger().log(Level.SEVERE, "An error occurred creating tables on the SQLite database: ", e);
|
||||
}
|
||||
initialize();
|
||||
}
|
||||
@@ -130,7 +145,9 @@ public class SQLite extends Database {
|
||||
.withZone(ZoneId.systemDefault())
|
||||
.format(Instant.now()).replaceAll(" ", "-") + ".db";
|
||||
final File databaseFile = new File(plugin.getDataFolder(), DATABASE_NAME + ".db");
|
||||
new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME).mkdirs();
|
||||
if (new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME).mkdirs()) {
|
||||
plugin.getLogger().info("Created backups directory in HuskHomes plugin data folder.");
|
||||
}
|
||||
final File backUpFile = new File(plugin.getDataFolder(), BACKUPS_FOLDER_NAME + File.separator + backupFileName);
|
||||
try {
|
||||
Files.copy(databaseFile.toPath(), backUpFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
Reference in new issue
Block a user