forked from TuxCoding/FastLogin
Prevent duplicate save requests
This commit is contained in:
@ -184,6 +184,8 @@ public class AuthStorage {
|
|||||||
try (Connection con = dataSource.getConnection()) {
|
try (Connection con = dataSource.getConnection()) {
|
||||||
String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);
|
String uuid = playerProfile.getOptId().map(UUIDAdapter::toMojangId).orElse(null);
|
||||||
|
|
||||||
|
playerProfile.getSaveLock().lock();
|
||||||
|
try {
|
||||||
if (playerProfile.isSaved()) {
|
if (playerProfile.isSaved()) {
|
||||||
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
|
try (PreparedStatement saveStmt = con.prepareStatement(UPDATE_PROFILE)) {
|
||||||
saveStmt.setString(1, uuid);
|
saveStmt.setString(1, uuid);
|
||||||
@ -210,6 +212,9 @@ public class AuthStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
playerProfile.getSaveLock().unlock();
|
||||||
|
}
|
||||||
} catch (SQLException ex) {
|
} catch (SQLException ex) {
|
||||||
core.getPlugin().getLog().error("Failed to save playerProfile {}", playerProfile, ex);
|
core.getPlugin().getLog().error("Failed to save playerProfile {}", playerProfile, ex);
|
||||||
}
|
}
|
||||||
|
@ -5,10 +5,12 @@ import com.github.games647.craftapi.model.Profile;
|
|||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
import java.util.concurrent.locks.ReentrantLock;
|
||||||
|
|
||||||
public class StoredProfile extends Profile {
|
public class StoredProfile extends Profile {
|
||||||
|
|
||||||
private long rowId;
|
private long rowId;
|
||||||
|
private final ReentrantLock saveLock = new ReentrantLock();
|
||||||
|
|
||||||
private boolean premium;
|
private boolean premium;
|
||||||
private String lastIp;
|
private String lastIp;
|
||||||
@ -27,6 +29,10 @@ public class StoredProfile extends Profile {
|
|||||||
this(-1, uuid, playerName, premium, lastIp, Instant.now());
|
this(-1, uuid, playerName, premium, lastIp, Instant.now());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ReentrantLock getSaveLock() {
|
||||||
|
return saveLock;
|
||||||
|
}
|
||||||
|
|
||||||
public synchronized boolean isSaved() {
|
public synchronized boolean isSaved() {
|
||||||
return rowId >= 0;
|
return rowId >= 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user