From be46da7085469ca34ecf648d8a503b3f0f34e871 Mon Sep 17 00:00:00 2001 From: Francisco Javier Tirado Sarti Date: Wed, 2 Aug 2023 22:41:29 +0200 Subject: [PATCH] [JBPM-10187] Sorting resources to avoid deadlock --- .../api/TransactionManagerHelper.java | 19 ++++++++++++------- .../drools/persistence/PersistableRunner.java | 4 ++++ .../jpa/OptimisticLockRetryInterceptor.java | 2 +- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/drools-persistence/drools-persistence-api/src/main/java/org/drools/persistence/api/TransactionManagerHelper.java b/drools-persistence/drools-persistence-api/src/main/java/org/drools/persistence/api/TransactionManagerHelper.java index adf917317dc..6d5f8abf967 100644 --- a/drools-persistence/drools-persistence-api/src/main/java/org/drools/persistence/api/TransactionManagerHelper.java +++ b/drools-persistence/drools-persistence-api/src/main/java/org/drools/persistence/api/TransactionManagerHelper.java @@ -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); @@ -41,7 +42,7 @@ public static void addToUpdatableSet(TransactionManager txm, Transformable trans } Set toBeUpdated = (Set) txm.getResource(APP_UPDETEABLE_RESOURCE); if (toBeUpdated == null) { - toBeUpdated = new LinkedHashSet(); + toBeUpdated = new LinkedHashSet<>(); txm.putResource(APP_UPDETEABLE_RESOURCE, toBeUpdated); } toBeUpdated.add(transformable); @@ -58,11 +59,15 @@ public static void removeFromUpdatableSet(TransactionManager txm, Transformable @SuppressWarnings("unchecked") public static Set getUpdateableSet(TransactionManager txm) { - Set toBeUpdated = (Set) txm.getResource(APP_UPDETEABLE_RESOURCE); - if (toBeUpdated == null) { - return Collections.emptySet(); + Set result = (Set) txm.getResource(APP_UPDETEABLE_RESOURCE); + if (result != null) { + SortedSet 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(toBeUpdated); + return Collections.emptySet(); } } diff --git a/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/PersistableRunner.java b/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/PersistableRunner.java index 0e7d302f7ff..39337d2fd69 100644 --- a/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/PersistableRunner.java +++ b/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/PersistableRunner.java @@ -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(); @@ -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(); diff --git a/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/OptimisticLockRetryInterceptor.java b/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/OptimisticLockRetryInterceptor.java index 9ba5fadcb38..e318dba30cf 100644 --- a/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/OptimisticLockRetryInterceptor.java +++ b/drools-persistence/drools-persistence-jpa/src/main/java/org/drools/persistence/jpa/OptimisticLockRetryInterceptor.java @@ -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 {