Game uses hikaricp for all connection pooling. Connection count set dynamically.

This commit is contained in:
2023-05-20 09:39:23 -04:00
parent 3c5ffed352
commit 0e4490576a
16 changed files with 136 additions and 184 deletions
+19 -18
View File
@@ -10,6 +10,7 @@
package engine.db.archive;
import engine.Enum;
import engine.gameManager.DbManager;
import engine.objects.Bane;
import engine.objects.City;
import engine.workthreads.WarehousePushThread;
@@ -134,9 +135,9 @@ public class BaneRecord extends DataRecord {
DateTime outDateTime = null;
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildDateTimeQueryStatement(connection, city);
ResultSet rs = statement.executeQuery()) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildDateTimeQueryStatement(connection, city);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
@@ -145,7 +146,7 @@ public class BaneRecord extends DataRecord {
}
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
return outDateTime;
@@ -166,13 +167,13 @@ public class BaneRecord extends DataRecord {
if (bane == null)
return;
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildUpdateLiveDateStatement(connection, bane, dateTime)) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildUpdateLiveDateStatement(connection, bane, dateTime)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
}
@@ -203,8 +204,8 @@ public class BaneRecord extends DataRecord {
public static void updateResolution(Bane bane, RecordEventType eventType) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildUpdateResolutionStatement(connection, bane, eventType)) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildUpdateResolutionStatement(connection, bane, eventType)) {
statement.execute();
@@ -223,9 +224,9 @@ public class BaneRecord extends DataRecord {
dividerString = "--------------------------------" + newLine;
queryString = "CALL `baneHistory`()";
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = connection.prepareCall(queryString);
ResultSet rs = statement.executeQuery()) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = connection.prepareCall(queryString);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
@@ -253,9 +254,9 @@ public class BaneRecord extends DataRecord {
WarehousePushThread.baneDelta = 0;
try (Connection localConnection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = localConnection.prepareStatement(queryString, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Make this an updatable result set as we'll reset the dirty flag as we go along
ResultSet rs = statement.executeQuery()) {
try (Connection localConnection = DbManager.getConnection();
PreparedStatement statement = localConnection.prepareStatement(queryString, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Make this an updatable result set as we'll reset the dirty flag as we go along
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
@@ -338,13 +339,13 @@ public class BaneRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildBaneInsertStatement(connection)) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildBaneInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
}
+10 -9
View File
@@ -10,6 +10,7 @@
package engine.db.archive;
import engine.Enum;
import engine.gameManager.DbManager;
import engine.objects.Guild;
import engine.objects.PlayerCharacter;
import engine.workthreads.WarehousePushThread;
@@ -109,13 +110,13 @@ public class CharacterRecord extends DataRecord {
public static void advanceKillCounter(PlayerCharacter player) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildKillCounterStatement(connection, player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
}
@@ -136,13 +137,13 @@ public class CharacterRecord extends DataRecord {
public static void advanceDeathCounter(PlayerCharacter player) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildDeathCounterStatement(connection, player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
}
@@ -163,13 +164,13 @@ public class CharacterRecord extends DataRecord {
public static void updatePromotionClass(PlayerCharacter player) {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildUpdatePromotionStatement(connection, player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
}
@@ -197,7 +198,7 @@ public class CharacterRecord extends DataRecord {
WarehousePushThread.charDelta = 0;
try (Connection localConnection = DataWarehouse.connectionPool.getConnection();
try (Connection localConnection = DbManager.getConnection();
PreparedStatement statement = localConnection.prepareStatement(queryString, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); // Make this an updatable result set as we'll reset the dirty flag as we go along
ResultSet rs = statement.executeQuery()) {
@@ -263,13 +264,13 @@ public class CharacterRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildCharacterInsertStatement(connection, this.player)) {
statement.execute();
} catch (SQLException e) {
Logger.error( "Error writing character record " + e.toString());
Logger.error("Error writing character record " + e.toString());
}
}
+3 -2
View File
@@ -10,6 +10,7 @@
package engine.db.archive;
import engine.Enum;
import engine.gameManager.DbManager;
import engine.objects.City;
import engine.workthreads.WarehousePushThread;
@@ -126,8 +127,8 @@ public class CityRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = this.buildCityInsertStatement(connection)) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = this.buildCityInsertStatement(connection)) {
statement.execute();
+4 -33
View File
@@ -12,6 +12,7 @@ package engine.db.archive;
import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;
import engine.gameManager.ConfigManager;
import engine.gameManager.DbManager;
import engine.util.Hasher;
import org.pmw.tinylog.Logger;
@@ -27,14 +28,13 @@ public class DataWarehouse implements Runnable {
public static final Hasher hasher = new Hasher("Cthulhu Owns Joo");
private static final LinkedBlockingQueue<DataRecord> recordQueue = new LinkedBlockingQueue<>();
public static HikariDataSource connectionPool = null;
public static HikariDataSource remoteConnectionPool = null;
public DataWarehouse() {
Logger.info("Configuring local Database Connection Pool...");
configureConnectionPool();
DbManager.configureConnectionPool();
// If WarehousePush is disabled
// then early exit
@@ -72,11 +72,7 @@ public class DataWarehouse implements Runnable {
String queryString;
String hashString;
try {
connection = DataWarehouse.connectionPool.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
connection = DbManager.getConnection();
if (connection == null) {
Logger.error("Null connection when writing zone hash.");
@@ -140,11 +136,7 @@ public class DataWarehouse implements Runnable {
String queryString;
ResultSet resultSet;
try {
connection = DataWarehouse.connectionPool.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
connection = DbManager.getConnection();
if (connection == null) {
Logger.error("Null connection during char record lookup");
@@ -282,27 +274,6 @@ public class DataWarehouse implements Runnable {
}
}
private static void configureConnectionPool() {
HikariConfig config = new HikariConfig();
config.setMaximumPoolSize(10);
config.setJdbcUrl("jdbc:mysql://" + ConfigManager.MB_DATABASE_ADDRESS.getValue() +
":" + ConfigManager.MB_DATABASE_PORT.getValue() + "/" +
ConfigManager.MB_DATABASE_NAME.getValue());
config.setUsername(ConfigManager.MB_DATABASE_USER.getValue());
config.setPassword( ConfigManager.MB_DATABASE_PASS.getValue());
config.addDataSourceProperty("characterEncoding", "utf8");
config.addDataSourceProperty("cachePrepStmts", "true");
config.addDataSourceProperty("prepStmtCacheSize", "250");
config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048");
connectionPool = new HikariDataSource(config); // setup the connection pool
Logger.info("Local warehouse database connection configured");
}
private static void configureRemoteConnectionPool() {
HikariConfig config = new HikariConfig();
+8 -7
View File
@@ -11,6 +11,7 @@ package engine.db.archive;
import engine.Enum;
import engine.Enum.RecordEventType;
import engine.gameManager.DbManager;
import engine.objects.Guild;
import engine.workthreads.WarehousePushThread;
@@ -164,16 +165,16 @@ public class GuildRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = this.buildGuildInsertStatement(connection)) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = this.buildGuildInsertStatement(connection)) {
statement.execute();
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
private PreparedStatement buildGuildInsertStatement(Connection connection) throws SQLException {
+2 -1
View File
@@ -10,6 +10,7 @@
package engine.db.archive;
import engine.Enum;
import engine.gameManager.DbManager;
import engine.objects.AbstractCharacter;
import engine.objects.Mine;
import engine.objects.PlayerCharacter;
@@ -131,7 +132,7 @@ public class MineRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = this.buildMineInsertStatement(connection)) {
statement.execute();
+16 -15
View File
@@ -9,6 +9,7 @@
package engine.db.archive;
import engine.gameManager.DbManager;
import engine.gameManager.ZoneManager;
import engine.math.Vector3fImmutable;
import engine.objects.Guild;
@@ -90,19 +91,19 @@ public class PvpRecord extends DataRecord {
LinkedList<Integer> outList = new LinkedList<>();
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildHistoryStatement(connection, charUUID, historyType);
ResultSet rs = statement.executeQuery()) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildHistoryStatement(connection, charUUID, historyType);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
switch (historyType) {
case KILLS:
outList.add((int) DataWarehouse.hasher.decrypt(rs.getString("victim_id"))[0]);
break;
case DEATHS:
outList.add((int) DataWarehouse.hasher.decrypt(rs.getString("char_id"))[0]);
break;
case KILLS:
outList.add((int) DataWarehouse.hasher.decrypt(rs.getString("victim_id"))[0]);
break;
case DEATHS:
outList.add((int) DataWarehouse.hasher.decrypt(rs.getString("char_id"))[0]);
break;
}
}
} catch (SQLException e) {
@@ -132,9 +133,9 @@ public class PvpRecord extends DataRecord {
outString = "[LUA_PVP() DATA WAREHOUSE]" + newLine;
dividerString = "--------------------------------" + newLine;
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildLuaHistoryQueryStatement(connection, charUUID);
ResultSet rs = statement.executeQuery()) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildLuaHistoryQueryStatement(connection, charUUID);
ResultSet rs = statement.executeQuery()) {
while (rs.next()) {
@@ -293,13 +294,13 @@ public class PvpRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
PreparedStatement statement = buildPvPInsertStatement(connection)) {
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = buildPvPInsertStatement(connection)) {
statement.execute();
} catch (SQLException e) {
Logger.error( e.toString());
Logger.error(e.toString());
}
// Warehouse record for this pvp event written if code path reaches here.
+2 -1
View File
@@ -10,6 +10,7 @@
package engine.db.archive;
import engine.Enum;
import engine.gameManager.DbManager;
import engine.objects.Realm;
import engine.workthreads.WarehousePushThread;
@@ -128,7 +129,7 @@ public class RealmRecord extends DataRecord {
public void write() {
try (Connection connection = DataWarehouse.connectionPool.getConnection();
try (Connection connection = DbManager.getConnection();
PreparedStatement statement = this.buildRealmInsertStatement(connection)) {
statement.execute();