Prevent duplicate save requests

This commit is contained in:
games647
2020-05-18 16:10:39 +02:00
parent 9941a69c6d
commit b1797c84d9
3 changed files with 49 additions and 38 deletions

View File

@ -184,6 +184,8 @@ public class AuthStorage {
try (Connection con = dataSource.getConnection()) {
String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);
playerProfile.getSaveLock().lock();
try {
if (playerProfile.isSaved()) {
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
saveStmt.setString(1, uuid);
@ -210,6 +212,9 @@ public class AuthStorage {
}
}
}
} finally {
playerProfile.getSaveLock().unlock();
}
} catch (SQLException ex) {
core.getPlugin().getLog().error("Failed to save playerProfile {}", playerProfile, ex);
}

View File

@ -5,10 +5,12 @@ import com.github.games647.craftapi.model.Profile;
import java.time.Instant;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.locks.ReentrantLock;
public class StoredProfile extends Profile {
private long rowId;
private final ReentrantLock saveLock = new ReentrantLock();
private boolean premium;
private String lastIp;
@ -27,6 +29,10 @@ public class StoredProfile extends Profile {
this(-1, uuid, playerName, premium, lastIp, Instant.now());
}
public ReentrantLock getSaveLock() {
return saveLock;
}
public synchronized boolean isSaved() {
return rowId >= 0;
}