Separate PG from MySQL chr saving
This commit is contained in:
@@ -1,19 +1,26 @@
|
||||
package database.character;
|
||||
|
||||
import client.Character;
|
||||
import database.PgDatabaseConnection;
|
||||
import database.monsterbook.MonsterCardRepository;
|
||||
import org.jdbi.v3.core.Handle;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import tools.DatabaseConnection;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
|
||||
public class CharacterSaver {
|
||||
private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class);
|
||||
private final PgDatabaseConnection pgConnection;
|
||||
private final MonsterCardRepository monsterCardRepository;
|
||||
|
||||
public CharacterSaver(MonsterCardRepository monsterCardRepository) {
|
||||
public CharacterSaver(PgDatabaseConnection pgConnection,
|
||||
MonsterCardRepository monsterCardRepository) {
|
||||
this.pgConnection = pgConnection;
|
||||
this.monsterCardRepository = monsterCardRepository;
|
||||
}
|
||||
|
||||
@@ -23,6 +30,12 @@ public class CharacterSaver {
|
||||
}
|
||||
|
||||
log.debug("Saving chr {}", chr.getName());
|
||||
saveToMysql(chr);
|
||||
saveToPostgres(chr);
|
||||
}
|
||||
|
||||
private void saveToMysql(Character chr) {
|
||||
Instant before = Instant.now();
|
||||
try (Connection con = DatabaseConnection.getConnection()) {
|
||||
con.setAutoCommit(false);
|
||||
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
|
||||
@@ -38,11 +51,24 @@ public class CharacterSaver {
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
log.error("Error saving chr {}, level: {}, job: {}", chr.getName(), chr.getLevel(), chr.getJob().getId(), e);
|
||||
return;
|
||||
}
|
||||
Duration saveDuration = Duration.between(before, Instant.now());
|
||||
log.debug("Saved {} to MySQL in {} ms", chr.getName(), saveDuration.toMillis());
|
||||
}
|
||||
|
||||
private void saveToPostgres(Character chr) {
|
||||
Instant before = Instant.now();
|
||||
|
||||
try (Handle handle = pgConnection.getHandle()) {
|
||||
handle.useTransaction(h -> doPostgresSave(h, chr));
|
||||
}
|
||||
|
||||
// Saving monster cards to both MySQL and Postgres for now
|
||||
monsterCardRepository.save(chr.getId(), chr.getMonsterBook().getCards());
|
||||
Duration saveDuration = Duration.between(before, Instant.now());
|
||||
log.debug("Saved {} to Postgres in {} ms", chr.getName(), saveDuration.toMillis());
|
||||
}
|
||||
|
||||
private void doPostgresSave(Handle handle, Character chr) {
|
||||
monsterCardRepository.save(handle, chr.getId(), chr.getMonsterBook().getCards());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ public class MonsterCardRepository {
|
||||
}
|
||||
}
|
||||
|
||||
public void save(int chrId, List<MonsterCard> cards) {
|
||||
try (Handle handle = connection.getHandle()) {
|
||||
public void save(Handle handle, int chrId, List<MonsterCard> cards) {
|
||||
try {
|
||||
PreparedBatch batch = handle.prepareBatch("""
|
||||
INSERT INTO monster_card (chr_id, card_id, level)
|
||||
VALUES (?, ?, ?)
|
||||
|
||||
Reference in New Issue
Block a user