Skip to content

Commit

Permalink
Removes usage of ConcurrentHashMap in LocalXAResource.java to avoid t…
Browse files Browse the repository at this point in the history
…hread pinning in JDKs of version 22 and lower (#8900)

Signed-off-by: Laird Nelson <laird.nelson@oracle.com>
  • Loading branch information
ljnelson authored Jun 21, 2024
1 parent b13a31c commit d5f8d58
Showing 1 changed file with 14 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<Xid, Association> ASSOCIATIONS = new ConcurrentHashMap<>();
// package-protected for testing only. Guarded by ASSOCIATIONS_LOCK below.
static final Map<Xid, Association> ASSOCIATIONS = new HashMap<>();

private static final Lock ASSOCIATIONS_LOCK = new ReentrantLock();


/*
Expand Down Expand Up @@ -362,10 +366,13 @@ private Association computeAssociation(XARoutine xaRoutine,
Xid xid,
BiFunction<? super Xid, ? super Association, ? extends Association> f)
throws XAException {
ASSOCIATIONS_LOCK.lock();
try {
return ASSOCIATIONS.compute(xid, f);
} catch (RuntimeException e) {
throw this.convert(xaRoutine, e);
} finally {
ASSOCIATIONS_LOCK.unlock();
}
}

Expand All @@ -375,13 +382,16 @@ private Association computeAssociation(XARoutine xaRoutine,
UnaryOperator<Association> f,
boolean removeAssociationOnError)
throws XAException {
ASSOCIATIONS_LOCK.lock();
try {
return ASSOCIATIONS.compute(xid, (x, a) -> remap(x, a, legalBranchStates, f));
} catch (RuntimeException e) {
if (removeAssociationOnError) {
ASSOCIATIONS.remove(xid);
}
throw this.convert(xaRoutine, e);
} finally {
ASSOCIATIONS_LOCK.unlock();
}
}

Expand Down

0 comments on commit d5f8d58

Please sign in to comment.