Skip to content

Commit

Permalink
Set serializable isolation when punishing
Browse files Browse the repository at this point in the history
  • Loading branch information
A248 committed Oct 26, 2023
1 parent 2985be9 commit d1df238
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ boolean wasNotRolledBack() {
return !rolledBack;
}

@Override
public void setIsolation(int level) {
try {
connection.setTransactionIsolation(level);
} catch (SQLException ex) {
throw new DataAccessException("Failed to set isolation", ex);
}
}

@Override
public void rollback() {
try {
Expand Down Expand Up @@ -88,6 +97,11 @@ private NestedTransaction(Savepoint savepoint) {
this.savepoint = Objects.requireNonNull(savepoint, "savepoint");
}

@Override
public void setIsolation(int level) {
throw new UnsupportedOperationException("Cannot set isolation levels in nested transactions");
}

@Override
public void rollback() {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@
*/
public interface Transaction {

/**
* Sets the isolation level on this transaction
*
* @param isolationLevel the isolation level
*/
void setIsolation(int isolationLevel);

/**
* Rolls back the enclosing scope
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ CentralisedFuture<Punishment> enactPunishment(DraftPunishment draftPunishment) {
creator);

return database.queryWithRetry((context, transaction) -> {
// Make sure concurrent executions do not conflict
transaction.setIsolation(Connection.TRANSACTION_SERIALIZABLE);

if (type != PunishmentType.KICK) {
database.clearExpiredPunishments(context, type, start);
}
Expand All @@ -124,6 +127,8 @@ CentralisedFuture<Punishment> calculatePunishment(CalculablePunishment calculabl

InternalDatabase database = dbProvider.get();
return database.queryWithRetry((context, transaction) -> {
// Make sure concurrent executions do not conflict
transaction.setIsolation(Connection.TRANSACTION_SERIALIZABLE);

var calculationResult = calculablePunishment.getCalculator().compute(
escalationTrack, victim,
Expand Down

0 comments on commit d1df238

Please sign in to comment.