Skip to content

Commit

Permalink
Fix CME in jdbc connection pool
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Pinčuk <alexander.v.pinchuk@gmail.com>
  • Loading branch information
avpinchuk committed May 4, 2023
1 parent 104dd6e commit 996900e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 31 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -19,6 +19,7 @@

import static java.util.logging.Level.FINEST;

import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;

import javax.security.auth.Subject;
Expand Down Expand Up @@ -57,7 +58,7 @@ public class ResourceHandle implements com.sun.appserv.connectors.internal.api.R
private int shareCount; // sharing within a component (XA only)
private boolean supportsXAResource;

private volatile boolean busy;
private AtomicBoolean busy = new AtomicBoolean(false);

private Subject subject;

Expand Down Expand Up @@ -350,12 +351,16 @@ public void enlistedInTransaction(Transaction transaction) throws IllegalStateEx
ConnectorRuntime.getRuntime().getPoolManager().resourceEnlisted(transaction, this);
}

public void setBusy(boolean isBusy) {
busy = isBusy;
public boolean isBusy() {
return busy.get();
}

public boolean isBusy() {
return busy;
public void setBusy(boolean busy) {
this.busy.set(busy);
}

public boolean trySetBusy(boolean busy) {
return this.busy.compareAndSet(!busy, busy);
}

public boolean getDestroyByLeakTimeOut() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 2022, 2023 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down Expand Up @@ -82,33 +82,15 @@ public int addResource(ResourceAllocator allocator, int count) throws PoolingExc
public ResourceHandle getResource() {
readLock.lock();
try {
// FIXME: masked concurrent modification exception. Collection may change when iterating.
for (int i = 0; i < resources.size(); i++) {
ResourceHandle h = resources.get(i);
if (!h.isBusy()) {
readLock.unlock();
writeLock.lock();
try {
if (!h.isBusy()) {
h.setBusy(true);
return h;
} else {
readLock.lock();
continue;
}
} finally {
writeLock.unlock();
for (ResourceHandle resource : resources) {
if (!resource.isBusy()) {
if (resource.trySetBusy(true)) {
return resource;
}
} else {
continue;
}
}
} finally {
try {
readLock.unlock();
} catch ( Exception e) {
//ignore
}
readLock.unlock();
}
return null;
}
Expand Down Expand Up @@ -163,10 +145,10 @@ public void removeAll() {
handler.deleteResource(it.next());
it.remove();
}
resources.clear();
} finally {
writeLock.unlock();
}
resources.clear();
}

@Override
Expand Down

0 comments on commit 996900e

Please sign in to comment.