New method to get MySQL connection for HikariCP

This commit is contained in:
ceze88 2022-08-07 16:36:30 +02:00
parent 1ab918a32e
commit 0ae9c6fcdb
3 changed files with 32 additions and 2 deletions

View File

@ -29,4 +29,6 @@ public interface DatabaseConnector {
interface ConnectionCallback { interface ConnectionCallback {
void accept(Connection connection) throws SQLException; void accept(Connection connection) throws SQLException;
} }
Connection getConnection();
} }

View File

@ -12,7 +12,7 @@ public class MySQLConnector implements DatabaseConnector {
private HikariDataSource hikari; private HikariDataSource hikari;
private boolean initializedSuccessfully; private boolean initializedSuccessfully;
public MySQLConnector(Plugin plugin, String hostname, int port, String database, String username, String password, boolean useSSL) { public MySQLConnector(Plugin plugin, String hostname, int port, String database, String username, String password, boolean useSSL, int poolSize) {
this.plugin = plugin; this.plugin = plugin;
plugin.getLogger().info("connecting to " + hostname + " : " + port); plugin.getLogger().info("connecting to " + hostname + " : " + port);
@ -21,7 +21,7 @@ public class MySQLConnector implements DatabaseConnector {
config.setJdbcUrl("jdbc:mysql://" + hostname + ":" + port + "/" + database + "?useSSL=" + useSSL); config.setJdbcUrl("jdbc:mysql://" + hostname + ":" + port + "/" + database + "?useSSL=" + useSSL);
config.setUsername(username); config.setUsername(username);
config.setPassword(password); config.setPassword(password);
config.setMaximumPoolSize(3); config.setMaximumPoolSize(poolSize);
try { try {
this.hikari = new HikariDataSource(config); this.hikari = new HikariDataSource(config);
@ -41,6 +41,7 @@ public class MySQLConnector implements DatabaseConnector {
this.hikari.close(); this.hikari.close();
} }
@Deprecated
@Override @Override
public void connect(ConnectionCallback callback) { public void connect(ConnectionCallback callback) {
try (Connection connection = this.hikari.getConnection()) { try (Connection connection = this.hikari.getConnection()) {
@ -50,4 +51,14 @@ public class MySQLConnector implements DatabaseConnector {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@Override
public Connection getConnection() {
try {
return this.hikari.getConnection();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
} }

View File

@ -39,6 +39,7 @@ public class SQLiteConnector implements DatabaseConnector {
} }
} }
@Deprecated
@Override @Override
public void connect(ConnectionCallback callback) { public void connect(ConnectionCallback callback) {
if (this.connection == null) { if (this.connection == null) {
@ -56,4 +57,20 @@ public class SQLiteConnector implements DatabaseConnector {
ex.printStackTrace(); ex.printStackTrace();
} }
} }
@Override
public Connection getConnection() {
try {
if (this.connection == null || this.connection.isClosed()) {
try {
this.connection = DriverManager.getConnection(this.connectionString);
} catch (SQLException ex) {
this.plugin.getLogger().severe("An error occurred retrieving the SQLite database connection: " + ex.getMessage());
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
return this.connection;
}
} }