Move transaction handling to CharacterSaver

This commit is contained in:
P0nk
2024-09-13 23:59:29 +02:00
parent 758347f7bc
commit 23a8a33e5f
2 changed files with 398 additions and 394 deletions

View File

@@ -8175,21 +8175,9 @@ public class Character extends AbstractCharacterObject {
} }
//ItemFactory saveItems and monsterbook.saveCards are the most time consuming here. //ItemFactory saveItems and monsterbook.saveCards are the most time consuming here.
public synchronized void saveCharToDB() { public synchronized void saveCharToDB(Connection con) throws SQLException {
if (!loggedIn) {
return;
}
Calendar c = Calendar.getInstance();
log.debug("Saving chr {}", name);
Server.getInstance().updateCharacterEntry(this); Server.getInstance().updateCharacterEntry(this);
try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false);
con.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
try {
try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, gachaexp = ?, hp = ?, mp = ?, maxhp = ?, maxmp = ?, sp = ?, ap = ?, gm = ?, skincolor = ?, gender = ?, job = ?, hair = ?, face = ?, map = ?, meso = ?, hpMpUsed = ?, spawnpoint = ?, party = ?, buddyCapacity = ?, messengerid = ?, messengerposition = ?, mountlevel = ?, mountexp = ?, mounttiredness= ?, equipslots = ?, useslots = ?, setupslots = ?, etcslots = ?, monsterbookcover = ?, vanquisherStage = ?, dojoPoints = ?, lastDojoStage = ?, finishedDojoTutorial = ?, vanquisherKills = ?, matchcardwins = ?, matchcardlosses = ?, matchcardties = ?, omokwins = ?, omoklosses = ?, omokties = ?, dataString = ?, fquest = ?, jailexpire = ?, partnerId = ?, marriageItemId = ?, lastExpGainTime = ?, ariantPoints = ?, partySearch = ? WHERE id = ?", Statement.RETURN_GENERATED_KEYS)) { try (PreparedStatement ps = con.prepareStatement("UPDATE characters SET level = ?, fame = ?, str = ?, dex = ?, luk = ?, `int` = ?, exp = ?, gachaexp = ?, hp = ?, mp = ?, maxhp = ?, maxmp = ?, sp = ?, ap = ?, gm = ?, skincolor = ?, gender = ?, job = ?, hair = ?, face = ?, map = ?, meso = ?, hpMpUsed = ?, spawnpoint = ?, party = ?, buddyCapacity = ?, messengerid = ?, messengerposition = ?, mountlevel = ?, mountexp = ?, mounttiredness= ?, equipslots = ?, useslots = ?, setupslots = ?, etcslots = ?, monsterbookcover = ?, vanquisherStage = ?, dojoPoints = ?, lastDojoStage = ?, finishedDojoTutorial = ?, vanquisherKills = ?, matchcardwins = ?, matchcardlosses = ?, matchcardties = ?, omokwins = ?, omoklosses = ?, omokties = ?, dataString = ?, fquest = ?, jailexpire = ?, partnerId = ?, marriageItemId = ?, lastExpGainTime = ?, ariantPoints = ?, partySearch = ? WHERE id = ?", Statement.RETURN_GENERATED_KEYS)) {
ps.setInt(1, level); // thanks CanIGetaPR for noticing an unnecessary "level" limitation when persisting DB data ps.setInt(1, level); // thanks CanIGetaPR for noticing an unnecessary "level" limitation when persisting DB data
ps.setInt(2, fame); ps.setInt(2, fame);
@@ -8566,18 +8554,6 @@ public class Character extends AbstractCharacterObject {
storage.saveToDB(con); storage.saveToDB(con);
usedStorage = false; usedStorage = false;
} }
con.commit();
} catch (Exception e) {
con.rollback();
throw e;
} finally {
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
con.setAutoCommit(true);
}
} catch (Exception e) {
log.error("Error saving chr {}, level: {}, job: {}", name, level, job.getId(), e);
}
} }
private void saveCooldowns(Connection con) throws SQLException { private void saveCooldowns(Connection con) throws SQLException {

View File

@@ -2,8 +2,15 @@ package database.character;
import client.Character; import client.Character;
import database.monsterbook.MonsterCardDao; import database.monsterbook.MonsterCardDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.DatabaseConnection;
import java.sql.Connection;
import java.sql.SQLException;
public class CharacterSaver { public class CharacterSaver {
private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class);
private final MonsterCardDao monsterCardDao; private final MonsterCardDao monsterCardDao;
public CharacterSaver(MonsterCardDao monsterCardDao) { public CharacterSaver(MonsterCardDao monsterCardDao) {
@@ -11,7 +18,28 @@ public class CharacterSaver {
} }
public void save(Character chr) { public void save(Character chr) {
chr.saveCharToDB(); if (!chr.isLoggedin()) {
return;
}
log.debug("Saving chr {}", chr.getName());
try (Connection con = DatabaseConnection.getConnection()) {
con.setAutoCommit(false);
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
try {
chr.saveCharToDB(con);
con.commit();
} catch (Exception e) {
con.rollback();
throw e;
} finally {
con.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
con.setAutoCommit(true);
}
} catch (SQLException e) {
log.error("Error saving chr {}, level: {}, job: {}", chr.getName(), chr.getLevel(), chr.getJob().getId(), e);
return;
}
// Saving monster cards to both MySQL and Postgres for now // Saving monster cards to both MySQL and Postgres for now
monsterCardDao.save(chr.getId(), chr.getMonsterBook().getCards()); monsterCardDao.save(chr.getId(), chr.getMonsterBook().getCards());