Skip to content
Snippets Groups Projects
Commit fecabb1d authored by Drahomír Karchňák's avatar Drahomír Karchňák
Browse files

#107 Fixed an issue, when two threads were trying to insert key/value into database

parent a0a8d3c3
No related branches found
No related tags found
No related merge requests found
......@@ -27,6 +27,8 @@ import cz.nic.tablexia.util.Log;
public class Game {
private static final String DATE_AND_TIME_FORMAT = "d.M.yyyy HH:mm:ss", DATE_FORMAT = "d.M.yyyy";
private static final Object ATOMIC_LOCK = new Object();
private long id;
private User user;
private GameDifficulty difficulty;
......@@ -181,19 +183,22 @@ public class Game {
}
public void setGameScore(final String key, final String value) {
final String storedValue = gameScoreMap.get(key);
new Thread(new Runnable() {
@Override
public void run() {
if (storedValue == null) {
if (GameScore.insertGameScore(Game.this.getId(), key, value)) {
gameScoreMap.put(key, value);
}
} else {
if (!storedValue.equals(value) && GameScore.updateGameScore(Game.this.getId(), key, value)) {
gameScoreMap.put(key, value);
}
}
synchronized (ATOMIC_LOCK) {
final String storedValue = gameScoreMap.get(key);
if (storedValue == null) {
if (GameScore.insertGameScore(Game.this.getId(), key, value)) {
gameScoreMap.put(key, value);
}
}
else {
if (!storedValue.equals(value) && GameScore.updateGameScore(Game.this.getId(), key, value)) {
gameScoreMap.put(key, value);
}
}
}
}
}).start();
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment