Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removes usage of ConcurrentHashMap in LocalXAResource.java to avoid thread pinning in JDKs of version 22 and lower #8900

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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