From 9ac21af21e6ae1eeaba3942c35fc79aff9a79c7e Mon Sep 17 00:00:00 2001 From: Laird Nelson Date: Thu, 20 Jun 2024 13:10:12 -0700 Subject: [PATCH] Removes usage of ConcurrentHashMap in LocalXAResource.java to avoid thread pinning in JDKs of version 22 and lower Signed-off-by: Laird Nelson --- .../integrations/jta/jdbc/LocalXAResource.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/integrations/jta/jdbc/src/main/java/io/helidon/integrations/jta/jdbc/LocalXAResource.java b/integrations/jta/jdbc/src/main/java/io/helidon/integrations/jta/jdbc/LocalXAResource.java index a48a24f9190..ddcffb175f0 100644 --- a/integrations/jta/jdbc/src/main/java/io/helidon/integrations/jta/jdbc/LocalXAResource.java +++ b/integrations/jta/jdbc/src/main/java/io/helidon/integrations/jta/jdbc/LocalXAResource.java @@ -18,9 +18,11 @@ import java.sql.Connection; import java.sql.SQLException; import java.util.EnumSet; +import java.util.HashMap; +import java.util.Map; import java.util.Objects; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.ConcurrentMap; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.UnaryOperator; @@ -64,8 +66,10 @@ final class LocalXAResource implements XAResource { private static final Xid[] EMPTY_XID_ARRAY = new Xid[0]; - // package-protected for testing only. - static final ConcurrentMap ASSOCIATIONS = new ConcurrentHashMap<>(); + // package-protected for testing only. Guarded by ASSOCIATIONS_LOCK below. + static final Map ASSOCIATIONS = new HashMap<>(); + + private static final Lock ASSOCIATIONS_LOCK = new ReentrantLock(); /* @@ -362,10 +366,13 @@ private Association computeAssociation(XARoutine xaRoutine, Xid xid, BiFunction f) throws XAException { + ASSOCIATIONS_LOCK.lock(); try { return ASSOCIATIONS.compute(xid, f); } catch (RuntimeException e) { throw this.convert(xaRoutine, e); + } finally { + ASSOCIATIONS_LOCK.unlock(); } } @@ -375,6 +382,7 @@ private Association computeAssociation(XARoutine xaRoutine, UnaryOperator f, boolean removeAssociationOnError) throws XAException { + ASSOCIATIONS_LOCK.lock(); try { return ASSOCIATIONS.compute(xid, (x, a) -> remap(x, a, legalBranchStates, f)); } catch (RuntimeException e) { @@ -382,6 +390,8 @@ private Association computeAssociation(XARoutine xaRoutine, ASSOCIATIONS.remove(xid); } throw this.convert(xaRoutine, e); + } finally { + ASSOCIATIONS_LOCK.unlock(); } }