diff --git a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/helper/ConcurrencyManager.java b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/helper/ConcurrencyManager.java index dbaa3705b1c..278e5fe3fe3 100644 --- a/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/helper/ConcurrencyManager.java +++ b/foundation/org.eclipse.persistence.core/src/org/eclipse/persistence/internal/helper/ConcurrencyManager.java @@ -15,7 +15,8 @@ // Oracle - initial API and implementation from Oracle TopLink package org.eclipse.persistence.internal.helper; -import java.io.*; +import java.io.Serializable; +import java.io.StringWriter; import java.security.AccessController; import java.util.*; import java.util.concurrent.ConcurrentHashMap; @@ -23,12 +24,14 @@ import java.util.concurrent.atomic.AtomicLong; import org.eclipse.persistence.config.SystemProperties; -import org.eclipse.persistence.exceptions.*; -import org.eclipse.persistence.internal.localization.*; +import org.eclipse.persistence.exceptions.ConcurrencyException; import org.eclipse.persistence.internal.identitymaps.CacheKey; +import org.eclipse.persistence.internal.localization.ToStringLocalization; +import org.eclipse.persistence.internal.localization.TraceLocalization; import org.eclipse.persistence.internal.security.PrivilegedAccessHelper; import org.eclipse.persistence.internal.security.PrivilegedGetSystemProperty; -import org.eclipse.persistence.logging.*; +import org.eclipse.persistence.logging.AbstractSessionLog; +import org.eclipse.persistence.logging.SessionLog; /** * INTERNAL: @@ -79,7 +82,7 @@ public class ConcurrencyManager implements Serializable { private static final Map THREADS_TO_WAIT_ON_ACQUIRE_NAME_OF_METHOD_CREATING_TRACE = new ConcurrentHashMap<>(); // Holds as a keys threads that needed to acquire one or more read locks on different cache keys. private static final Map READ_LOCK_MANAGERS = new ConcurrentHashMap<>(); - private static final Set THREADS_WAITING_TO_RELEASE_DEFERRED_LOCKS = new HashSet<>(); + private static final Set THREADS_WAITING_TO_RELEASE_DEFERRED_LOCKS = ConcurrentHashMap.newKeySet(); private static final Map THREADS_WAITING_TO_RELEASE_DEFERRED_LOCKS_BUILD_OBJECT_COMPLETE_GOES_NOWHERE = new ConcurrentHashMap<>(); private static final String ACQUIRE_METHOD_NAME = ConcurrencyManager.class.getName() + ".acquire(...)"; @@ -209,7 +212,8 @@ public synchronized boolean acquireWithWait(boolean forMerge, int wait) throws C } finally { removeThreadNoLongerWaitingToAcquireLockForWriting(currentThread); } - if ((this.activeThread == null && this.numberOfReaders.get() == 0) || (this.activeThread == currentThread)) { + if ((this.activeThread == null && this.numberOfReaders.get() == 0) + || (this.activeThread == currentThread)) { acquire(forMerge); return true; } @@ -375,7 +379,7 @@ public Thread getActiveThread() { * Return the deferred lock manager from the thread */ public static DeferredLockManager getDeferredLockManager(Thread thread) { - return (thread == null) ? null : getDeferredLockManagers().get(thread); + return getDeferredLockManagers().get(thread); } /** @@ -458,7 +462,8 @@ public static boolean isBuildObjectOnThreadComplete(Thread thread, Map recursive } Vector deferredLocks = lockManager.getDeferredLocks(); - for (Enumeration deferredLocksEnum = deferredLocks.elements(); deferredLocksEnum.hasMoreElements();) { + for (Enumeration deferredLocksEnum = deferredLocks.elements(); + deferredLocksEnum.hasMoreElements();) { ConcurrencyManager deferedLock = (ConcurrencyManager)deferredLocksEnum.nextElement(); Thread activeThread = null; if (deferedLock.isAcquired()) { @@ -808,6 +813,7 @@ protected static Map getReadLockManagers() { /** * Print the nested depth. */ + @Override public String toString() { Object[] args = { Integer.valueOf(getDepth()) }; return Helper.getShortClassName(getClass()) + ToStringLocalization.buildMessage("nest_level", args); @@ -852,7 +858,16 @@ private static String getPropertyRecordStackOnLock() { * */ public void putThreadAsWaitingToAcquireLockForWriting(Thread thread, String methodName) { + THREADS_TO_WAIT_ON_ACQUIRE.put(thread, this); THREADS_TO_WAIT_ON_ACQUIRE_NAME_OF_METHOD_CREATING_TRACE.put(thread, methodName); + } + + /** + * The thread has acquired the lock for writing or decided to defer acquiring the lock putting this lock into its + * deferred lock list. + */ + public void removeThreadNoLongerWaitingToAcquireLockForWriting(Thread thread) { + THREADS_TO_WAIT_ON_ACQUIRE.remove(thread); THREADS_TO_WAIT_ON_ACQUIRE_NAME_OF_METHOD_CREATING_TRACE.remove(thread); } @@ -873,14 +888,6 @@ public void removeThreadNoLongerWaitingToAcquireLockForReading(Thread thread) { THREADS_TO_WAIT_ON_ACQUIRE_READ_LOCK_NAME_OF_METHOD_CREATING_TRACE.remove(thread); } - /** - * The thread has acquired the lock for writing or decided to defer acquiring the lock putting this lock into its - * deferred lock list. - */ - public void removeThreadNoLongerWaitingToAcquireLockForWriting(Thread thread) { - THREADS_TO_WAIT_ON_ACQUIRE.remove(thread); - } - /** Getter for {@link #concurrencyManagerId} */ public long getConcurrencyManagerId() { return concurrencyManagerId; @@ -982,10 +989,7 @@ protected static ReadLockManager getReadLockManagerEnsureResultIsNotNull(Thread Map readLockManagers = getReadLockManagers(); if (!readLockManagers.containsKey(thread)) { ReadLockManager readLockManager = new ReadLockManager(); - ReadLockManager value = readLockManagers.get(thread); - if (value == null) { - readLockManagers.put(thread, readLockManager); - } + readLockManagers.putIfAbsent(thread, readLockManager); return readLockManager; } return readLockManagers.get(thread);