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

@ -18,7 +18,6 @@ import net.coreprotect.consumer.process.Process;
public class Consumer extends Process implements Runnable, Thread.UncaughtExceptionHandler {
private static Thread consumerThread = null;
public static volatile boolean resetConnection = false;
public static volatile int currentConsumer = 0;
public static volatile boolean isPaused = false;
public static volatile boolean transacting = false;
@ -103,12 +102,6 @@ public class Consumer extends Process implements Runnable, Thread.UncaughtExcept
try {
while ((ConfigHandler.serverRunning || ConfigHandler.converterRunning) && (Consumer.isPaused || ConfigHandler.purgeRunning || Consumer.consumer_id.get(process_id)[1] == 1)) {
pausedSuccess = true;
if (Consumer.isPaused || ConfigHandler.purgeRunning) {
if (connection != null) {
connection.close();
connection = null;
}
}
Thread.sleep(100);
}
}

View file

@ -2,6 +2,7 @@ package net.coreprotect.consumer.process;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Locale;
@ -45,36 +46,13 @@ public class Process {
public static final int BLOCKDATA_INSERT = 25;
public static final int ITEM_TRANSACTION = 26;
protected static Connection connection = null;
public static int lastLockUpdate = 0;
private static int lastConnection = 0;
private static volatile int currentConsumerSize = 0;
public static int getCurrentConsumerSize() {
return currentConsumerSize;
}
private static void validateConnection(boolean lastRun) {
try {
if (connection != null) {
int timeSinceLastConnection = ((int) (System.currentTimeMillis() / 1000L)) - lastConnection;
if ((!lastRun && timeSinceLastConnection > 900) || !connection.isValid(5) || Consumer.resetConnection) {
connection.close();
connection = null;
Consumer.resetConnection = false;
}
}
if (connection == null && ConfigHandler.serverRunning) {
connection = Database.getConnection(false, 500);
lastConnection = (int) (System.currentTimeMillis() / 1000L);
}
}
catch (Exception e) {
e.printStackTrace();
}
}
protected static void updateLockTable(Statement statement, int locked) {
try {
int unixTimestamp = (int) (System.currentTimeMillis() / 1000L);
@ -90,9 +68,7 @@ public class Process {
}
protected static void processConsumer(int processId, boolean lastRun) {
try {
// Connection
validateConnection(lastRun);
try (Connection connection = Database.getConnection(false, 500)) {
if (connection == null) {
return;
}
@ -243,7 +219,6 @@ public class Process {
for (int index = (i - 1); index >= 0; index--) {
consumerData.remove(index);
}
connection = null;
currentConsumerSize = 0;
Consumer.isPaused = false;
return;
@ -288,11 +263,6 @@ public class Process {
users.clear();
consumerObject.clear();
consumerData.clear();
if (lastRun) {
connection.close();
connection = null;
}
}
catch (Exception e) {
e.printStackTrace();