mirror of
https://github.com/haveno-dex/haveno.git
synced 2025-07-30 02:08:58 -04:00
Reset persisted data if there is a serialisation problem #319
This commit is contained in:
parent
39524719ee
commit
25cac42700
1 changed files with 22 additions and 13 deletions
|
@ -60,6 +60,7 @@ public class Persistence {
|
||||||
private final File dir;
|
private final File dir;
|
||||||
private final String prefix;
|
private final String prefix;
|
||||||
private final File storageFile;
|
private final File storageFile;
|
||||||
|
private int resetCounter = 0;
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////////////////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
|
@ -83,17 +84,10 @@ public class Persistence {
|
||||||
lock.lock();
|
lock.lock();
|
||||||
final Map<String, Serializable> map = readRootMap();
|
final Map<String, Serializable> map = readRootMap();
|
||||||
|
|
||||||
if (map == null) {
|
if (map == null)
|
||||||
lock.lock();
|
|
||||||
try {
|
|
||||||
saveObjectToFile((Serializable) rootMap);
|
saveObjectToFile((Serializable) rootMap);
|
||||||
} finally {
|
else
|
||||||
lock.unlock();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
rootMap = map;
|
rootMap = map;
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
lock.unlock();
|
lock.unlock();
|
||||||
}
|
}
|
||||||
|
@ -213,11 +207,26 @@ public class Persistence {
|
||||||
log.trace("File not found is ok for the first execute.");
|
log.trace("File not found is ok for the first execute.");
|
||||||
return null;
|
return null;
|
||||||
} catch (ClassNotFoundException | IOException e2) {
|
} catch (ClassNotFoundException | IOException e2) {
|
||||||
|
log.warn("Could not read rootMap. " + e2);
|
||||||
|
|
||||||
|
// If there are problems with incompatible versions, we reset the persisted data
|
||||||
|
// TODO We need a clean solution when we use another persistence solution
|
||||||
|
rootMap = new HashMap<>();
|
||||||
|
saveObjectToFile((Serializable) rootMap);
|
||||||
|
resetCounter++;
|
||||||
|
|
||||||
|
// We only try that once, if it fails repeatedly we
|
||||||
|
if (resetCounter == 1) {
|
||||||
|
log.warn("We reset the persisted data and try again to read the root map.");
|
||||||
|
return readRootMap();
|
||||||
|
}
|
||||||
|
else {
|
||||||
e2.printStackTrace();
|
e2.printStackTrace();
|
||||||
log.error("Could not read rootMap. " + e2);
|
log.error("We tried already to reset the persisted data, but we get an error again, so we give up.");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void saveObjectToFile(Serializable serializable) {
|
private void saveObjectToFile(Serializable serializable) {
|
||||||
File tempFile = null;
|
File tempFile = null;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue