Skip to content

Commit

Permalink
Sorting the transformable set to avoid deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Aug 2, 2023
1 parent 02b43c7 commit 5054663
Showing 1 changed file with 17 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@
package org.drools.persistence.api;

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.TreeSet;

public class TransactionManagerHelper {

private static final String APP_UPDETEABLE_RESOURCE = "app-updateable-resource";
private static final String CMD_UPDETEABLE_RESOURCE = "cmd-updateable-resource";

public static void registerTransactionSyncInContainer(TransactionManager txm, OrderedTransactionSynchronization synchronization) {
TransactionSynchronizationContainer container = (TransactionSynchronizationContainer)txm.getResource(TransactionSynchronizationContainer.RESOURCE_KEY);
Expand All @@ -39,17 +40,26 @@ public static void addToUpdatableSet(TransactionManager txm, Transformable trans
if (transformable == null) {
return;
}
Set<Transformable> toBeUpdated = (Set<Transformable>) txm.getResource(APP_UPDETEABLE_RESOURCE);
Set<Transformable> toBeUpdated = (Set<Transformable>) txm
.getResource(APP_UPDETEABLE_RESOURCE);
if (toBeUpdated == null) {
toBeUpdated = new LinkedHashSet<Transformable>();

toBeUpdated = new TreeSet<>(new Comparator<Transformable>() {
@Override
public int compare(Transformable o1, Transformable o2) {
int result = o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
return result == 0 ? -1 : result;
}
});
txm.putResource(APP_UPDETEABLE_RESOURCE, toBeUpdated);
}
toBeUpdated.add(transformable);
}

@SuppressWarnings("unchecked")
public static void removeFromUpdatableSet(TransactionManager txm, Transformable transformable) {
Set<Transformable> toBeUpdated = (Set<Transformable>) txm.getResource(APP_UPDETEABLE_RESOURCE);
Set<Transformable> toBeUpdated = (Set<Transformable>) txm
.getResource(APP_UPDETEABLE_RESOURCE);
if (toBeUpdated == null) {
return;
}
Expand All @@ -58,11 +68,11 @@ public static void removeFromUpdatableSet(TransactionManager txm, Transformable

@SuppressWarnings("unchecked")
public static Set<Transformable> getUpdateableSet(TransactionManager txm) {
Set<Transformable> toBeUpdated = (Set<Transformable>) txm.getResource(APP_UPDETEABLE_RESOURCE);
Set<Transformable> toBeUpdated = (Set<Transformable>) txm
.getResource(APP_UPDETEABLE_RESOURCE);
if (toBeUpdated == null) {
return Collections.emptySet();
}

return new LinkedHashSet<Transformable>(toBeUpdated);
return new LinkedHashSet<>(toBeUpdated);
}
}

0 comments on commit 5054663

Please sign in to comment.