Move transaction handling to CharacterSaver
This commit is contained in:
@@ -2,8 +2,15 @@ package database.character;
|
||||
|
||||
import client.Character;
|
||||
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 {
|
||||
private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class);
|
||||
private final MonsterCardDao monsterCardDao;
|
||||
|
||||
public CharacterSaver(MonsterCardDao monsterCardDao) {
|
||||
@@ -11,7 +18,28 @@ public class CharacterSaver {
|
||||
}
|
||||
|
||||
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
|
||||
monsterCardDao.save(chr.getId(), chr.getMonsterBook().getCards());
|
||||
|
||||
Reference in New Issue
Block a user