Skip to content

Commit ec96db2

Browse files
committed
Fix duplicate key violation
Affects issues: - Fixed #3956
1 parent 3a5f783 commit ec96db2

1 file changed

Lines changed: 30 additions & 13 deletions

File tree

Plan/common/src/main/java/com/djrapitops/plan/storage/database/transactions/events/StoreAllowlistBounceTransaction.java

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package com.djrapitops.plan.storage.database.transactions.events;
1818

19+
import com.djrapitops.plan.exceptions.database.DBOpException;
1920
import com.djrapitops.plan.identification.ServerUUID;
2021
import com.djrapitops.plan.storage.database.sql.tables.AllowlistBounceTable;
2122
import com.djrapitops.plan.storage.database.transactions.ExecStatement;
@@ -48,25 +49,41 @@ public StoreAllowlistBounceTransaction(UUID playerUUID, @Untrusted String player
4849

4950
@Override
5051
protected void performOperations() {
51-
boolean updated = execute(new ExecStatement(AllowlistBounceTable.INCREMENT_TIMES_STATEMENT) {
52+
boolean updated = update();
53+
if (!updated) {
54+
try {
55+
insert();
56+
} catch (DBOpException e) {
57+
if (e.isDuplicateKeyViolation()) {
58+
update();
59+
return;
60+
}
61+
throw e;
62+
}
63+
}
64+
}
65+
66+
private void insert() {
67+
execute(new ExecStatement(AllowlistBounceTable.INSERT_STATEMENT) {
68+
@Override
69+
public void prepare(PreparedStatement statement) throws SQLException {
70+
statement.setString(1, playerUUID.toString());
71+
statement.setString(2, playerName);
72+
statement.setString(3, serverUUID.toString());
73+
statement.setInt(4, 1);
74+
statement.setLong(5, time);
75+
}
76+
});
77+
}
78+
79+
private boolean update() {
80+
return execute(new ExecStatement(AllowlistBounceTable.INCREMENT_TIMES_STATEMENT) {
5281
@Override
5382
public void prepare(PreparedStatement statement) throws SQLException {
5483
statement.setLong(1, time);
5584
statement.setString(2, playerUUID.toString());
5685
statement.setString(3, serverUUID.toString());
5786
}
5887
});
59-
if (!updated) {
60-
execute(new ExecStatement(AllowlistBounceTable.INSERT_STATEMENT) {
61-
@Override
62-
public void prepare(PreparedStatement statement) throws SQLException {
63-
statement.setString(1, playerUUID.toString());
64-
statement.setString(2, playerName);
65-
statement.setString(3, serverUUID.toString());
66-
statement.setInt(4, 1);
67-
statement.setLong(5, time);
68-
}
69-
});
70-
}
7188
}
7289
}

0 commit comments

Comments
 (0)