Add HikariCP connection pool for MySQL (#86)

* Add HikariCP connection pool for MySQL

* Make code more compatible with connection pooling

- Remove connection caching in Process/Consumer
- Use try-with-resources to make sure Connections always get closed, even in
  when an Exception occurs.

* Disable SSL for MySQL
This commit is contained in:
Jan Erik Petersen 2021-11-16 01:57:33 +01:00 committed by GitHub
parent 2d90f9cc08
commit fc99c24a0a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 157 additions and 200 deletions

View file

@ -138,15 +138,7 @@ public class Database extends Queue {
}
if (Config.getGlobal().MYSQL) {
try {
/* Using useServerPrepStmts, cachePrepStmts, and rewriteBatchedStatements per https://github.com/brettwooldridge/HikariCP/wiki/MySQL-Configuration */
String database = "jdbc:mysql://" + ConfigHandler.host + ":" + ConfigHandler.port + "/" + ConfigHandler.database + "?useUnicode=true&characterEncoding=utf-8&connectTimeout=10000&useSSL=false&allowPublicKeyRetrieval=true&useCursorFetch=true&useLocalSessionState=true&rewriteBatchedStatements=true&maintainTimeStats=false";
connection = DriverManager.getConnection(database, ConfigHandler.username, ConfigHandler.password);
/* Recommended implementation per https://dev.mysql.com/doc/refman/5.0/en/charset-applications.html & https://dev.mysql.com/doc/refman/5.0/en/charset-syntax.html */
Statement statement = connection.createStatement();
statement.executeUpdate("SET NAMES 'utf8mb4'"); // COLLATE 'utf8mb4mb4_general_ci'
statement.close();
connection = ConfigHandler.hikariDataSource.getConnection();
ConfigHandler.databaseReachable = true;
}
catch (Exception e) {
@ -316,8 +308,7 @@ public class Database extends Queue {
if (Config.getGlobal().MYSQL) {
boolean success = false;
try {
Connection connection = Database.getConnection(true, true, true, 0);
try (Connection connection = Database.getConnection(true, true, true, 0)) {
if (connection != null) {
String index = "";
Statement statement = connection.createStatement();
@ -357,7 +348,6 @@ public class Database extends Queue {
initializeTables(prefix, statement);
}
statement.close();
connection.close();
success = true;
}
}
@ -369,8 +359,7 @@ public class Database extends Queue {
}
}
if (!Config.getGlobal().MYSQL) {
try {
Connection connection = Database.getConnection(true, 0);
try (Connection connection = Database.getConnection(true, 0)) {
Statement statement = connection.createStatement();
List<String> tableData = new ArrayList<>();
List<String> indexData = new ArrayList<>();
@ -553,7 +542,6 @@ public class Database extends Queue {
initializeTables(prefix, statement);
}
statement.close();
connection.close();
}
catch (Exception e) {
e.printStackTrace();

View file

@ -18,7 +18,7 @@ public class BlockLookupAPI {
public static List<String[]> performLookup(Block block, int offset) {
List<String[]> result = new ArrayList<>();
try {
try (Connection connection = Database.getConnection(false, 1000)) {
if (block == null) {
return result;
}
@ -33,7 +33,6 @@ public class BlockLookupAPI {
checkTime = time - offset;
}
Connection connection = Database.getConnection(false, 1000);
if (connection == null) {
return result;
}
@ -62,7 +61,6 @@ public class BlockLookupAPI {
}
results.close();
statement.close();
connection.close();
}
catch (Exception e) {
e.printStackTrace();