Skip to content

Commit

Permalink
[JBPM-10187] Sorting resources to avoid deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
fjtirado committed Aug 10, 2023
1 parent 5b13798 commit be46da7
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SortedSet;
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 @@ -41,7 +42,7 @@ public static void addToUpdatableSet(TransactionManager txm, Transformable trans
}
Set<Transformable> toBeUpdated = (Set<Transformable>) txm.getResource(APP_UPDETEABLE_RESOURCE);
if (toBeUpdated == null) {
toBeUpdated = new LinkedHashSet<Transformable>();
toBeUpdated = new LinkedHashSet<>();
txm.putResource(APP_UPDETEABLE_RESOURCE, toBeUpdated);
}
toBeUpdated.add(transformable);
Expand All @@ -58,11 +59,15 @@ 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);
if (toBeUpdated == null) {
return Collections.emptySet();
Set<Transformable> result = (Set<Transformable>) txm.getResource(APP_UPDETEABLE_RESOURCE);
if (result != null) {
SortedSet<Transformable> sorted = new TreeSet<>((o1, o2) -> {
int compared = o1.getClass().getSimpleName().compareTo(o2.getClass().getSimpleName());
return compared == 0 ? 1 : compared;
});
sorted.addAll(result);
return sorted;
}

return new LinkedHashSet<Transformable>(toBeUpdated);
return Collections.emptySet();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ public void afterCompletion(int status) {
this.service.jpm.endCommandScopedEntityManager();

KieSession ksession = this.service.ksession;
logger.debug("Cleaning up session {} information", ksession != null ? ksession.getIdentifier() : null);
// clean up cached process and work item instances
if ( ksession != null ) {
InternalProcessRuntime internalProcessRuntime = ((InternalWorkingMemory) ksession).internalGetProcessRuntime();
Expand All @@ -513,8 +514,11 @@ public void afterCompletion(int status) {
}

internalProcessRuntime.clearProcessInstances();
logger.debug("Cached process instances after clean up {}",internalProcessRuntime.getProcessInstances());
}

((JPAWorkItemManager) ksession.getWorkItemManager()).clearWorkItems();

}
if (status != TransactionManager.STATUS_COMMITTED) {
this.service.jpm.resetApplicationScopedPersistenceContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ protected RequestContext internalExecute( Executable executable, RequestContext
RuntimeException originException = null;

while (true) {
if (attempt > 1) {
if (attempt > 0) {
logger.trace("retrying (attempt {})...", attempt);
}
try {
Expand Down

0 comments on commit be46da7

Please sign in to comment.