Fixed possible "Database is closed" exceptions on ShutdownHook #546

This commit is contained in:
Rsl1122 2018-04-02 11:18:57 +03:00
parent 84eeda66b2
commit bf77d6edfa

View File

@ -56,10 +56,6 @@ public class ShutdownHook extends Thread {
Map<UUID, Session> activeSessions = SessionCache.getActiveSessions();
long now = MiscUtils.getTime();
db = Database.getActive();
if (!db.isOpen()) {
db.init();
}
saveFirstSessionInformation(db, now);
saveActiveSessions(db, activeSessions, now);
} catch (IllegalStateException ignored) {
@ -78,9 +74,12 @@ public class ShutdownHook extends Thread {
}
}
private void saveFirstSessionInformation(Database db, long now) {
private void saveFirstSessionInformation(Database db, long now) throws DBInitException {
DataCache dataCache = CacheSystem.getInstance().getDataCache();
for (Map.Entry<UUID, Integer> entry : dataCache.getFirstSessionMsgCounts().entrySet()) {
if (!db.isOpen()) {
db.init();
}
try {
UUID uuid = entry.getKey();
int messagesSent = entry.getValue();
@ -91,7 +90,7 @@ public class ShutdownHook extends Thread {
}
}
private void saveActiveSessions(Database db, Map<UUID, Session> activeSessions, long now) {
private void saveActiveSessions(Database db, Map<UUID, Session> activeSessions, long now) throws DBInitException {
for (Map.Entry<UUID, Session> entry : activeSessions.entrySet()) {
UUID uuid = entry.getKey();
Session session = entry.getValue();
@ -99,6 +98,9 @@ public class ShutdownHook extends Thread {
if (sessionEnd == -1) {
session.endSession(now);
}
if (!db.isOpen()) {
db.init();
}
try {
Log.debug("Shutdown: Saving a session: " + session.getSessionStart());
db.save().session(uuid, session);