diff --git a/Core/src/main/java/com/songoda/core/database/DatabaseConnector.java b/Core/src/main/java/com/songoda/core/database/DatabaseConnector.java index 2ca3220c..7d0c0ff2 100644 --- a/Core/src/main/java/com/songoda/core/database/DatabaseConnector.java +++ b/Core/src/main/java/com/songoda/core/database/DatabaseConnector.java @@ -29,4 +29,6 @@ public interface DatabaseConnector { interface ConnectionCallback { void accept(Connection connection) throws SQLException; } + + Connection getConnection(); } diff --git a/Core/src/main/java/com/songoda/core/database/MySQLConnector.java b/Core/src/main/java/com/songoda/core/database/MySQLConnector.java index e0a4cbc8..1b2fe4c1 100644 --- a/Core/src/main/java/com/songoda/core/database/MySQLConnector.java +++ b/Core/src/main/java/com/songoda/core/database/MySQLConnector.java @@ -12,7 +12,7 @@ public class MySQLConnector implements DatabaseConnector { private HikariDataSource hikari; 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; 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.setUsername(username); config.setPassword(password); - config.setMaximumPoolSize(3); + config.setMaximumPoolSize(poolSize); try { this.hikari = new HikariDataSource(config); @@ -41,6 +41,7 @@ public class MySQLConnector implements DatabaseConnector { this.hikari.close(); } + @Deprecated @Override public void connect(ConnectionCallback callback) { try (Connection connection = this.hikari.getConnection()) { @@ -50,4 +51,14 @@ public class MySQLConnector implements DatabaseConnector { ex.printStackTrace(); } } + + @Override + public Connection getConnection() { + try { + return this.hikari.getConnection(); + } catch (Exception ex) { + ex.printStackTrace(); + } + return null; + } } diff --git a/Core/src/main/java/com/songoda/core/database/SQLiteConnector.java b/Core/src/main/java/com/songoda/core/database/SQLiteConnector.java index e5878286..2a777a49 100644 --- a/Core/src/main/java/com/songoda/core/database/SQLiteConnector.java +++ b/Core/src/main/java/com/songoda/core/database/SQLiteConnector.java @@ -39,6 +39,7 @@ public class SQLiteConnector implements DatabaseConnector { } } + @Deprecated @Override public void connect(ConnectionCallback callback) { if (this.connection == null) { @@ -56,4 +57,20 @@ public class SQLiteConnector implements DatabaseConnector { 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; + } }