Skip to content

Introduce interface cache for proxied EntityManager and Query types #23345

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

Merged
merged 1 commit into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
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 @@ -43,6 +43,7 @@
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ConcurrentReferenceHashMap;

/**
* Delegate for creating a variety of {@link javax.persistence.EntityManager}
Expand All @@ -65,6 +66,7 @@
*
* @author Juergen Hoeller
* @author Rod Johnson
* @author Mark Paluch
* @since 2.0
* @see javax.persistence.EntityManagerFactory#createEntityManager()
* @see javax.persistence.PersistenceContextType#EXTENDED
Expand All @@ -73,6 +75,8 @@
*/
public abstract class ExtendedEntityManagerCreator {

private static final Map<Class<?>, Class[]> CACHED_ENTITY_MANAGER_INTERFACES = new ConcurrentReferenceHashMap<>();

/**
* Create an application-managed extended EntityManager proxy.
* @param rawEntityManager the raw EntityManager to decorate
Expand Down Expand Up @@ -222,17 +226,29 @@ private static EntityManager createProxy(
boolean containerManaged, boolean synchronizedWithTransaction) {

Assert.notNull(rawEm, "EntityManager must not be null");
Set<Class<?>> ifcs = new LinkedHashSet<>();
Class[] interfaces;

if (emIfc != null) {
ifcs.add(emIfc);
interfaces = CACHED_ENTITY_MANAGER_INTERFACES.computeIfAbsent(emIfc, key -> {
Set<Class<?>> ifcs = new LinkedHashSet<>();
ifcs.add(key);
ifcs.add(EntityManagerProxy.class);
return ClassUtils.toClassArray(ifcs);
});
}
else {
ifcs.addAll(ClassUtils.getAllInterfacesForClassAsSet(rawEm.getClass(), cl));
interfaces = CACHED_ENTITY_MANAGER_INTERFACES.computeIfAbsent(rawEm.getClass(), key -> {
Set<Class<?>> ifcs = new LinkedHashSet<>();
ifcs.addAll(ClassUtils
.getAllInterfacesForClassAsSet(key, cl));
ifcs.add(EntityManagerProxy.class);
return ClassUtils.toClassArray(ifcs);
});
}
ifcs.add(EntityManagerProxy.class);

return (EntityManager) Proxy.newProxyInstance(
(cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()),
ClassUtils.toClassArray(ifcs),
interfaces,
new ExtendedEntityManagerInvocationHandler(
rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.springframework.transaction.support.TransactionSynchronizationManager;
import org.springframework.util.ClassUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ConcurrentReferenceHashMap;

/**
* Delegate for creating a shareable JPA {@link javax.persistence.EntityManager}
Expand All @@ -59,6 +60,7 @@
* @author Juergen Hoeller
* @author Rod Johnson
* @author Oliver Gierke
* @author Mark Paluch
* @since 2.0
* @see javax.persistence.PersistenceContext
* @see javax.persistence.PersistenceContextType#TRANSACTION
Expand All @@ -73,6 +75,8 @@ public abstract class SharedEntityManagerCreator {

private static final Set<String> queryTerminatingMethods = new HashSet<>(8);

private static final Map<Class<?>, Class[]> CACHED_QUERY_INTERFACES = new ConcurrentReferenceHashMap<>();

static {
transactionRequiringMethods.add("joinTransaction");
transactionRequiringMethods.add("flush");
Expand Down Expand Up @@ -310,7 +314,8 @@ else if (transactionRequiringMethods.contains(method.getName())) {
if (result instanceof Query) {
Query query = (Query) result;
if (isNewEm) {
Class<?>[] ifcs = ClassUtils.getAllInterfacesForClass(query.getClass(), this.proxyClassLoader);
Class<?>[] ifcs = CACHED_QUERY_INTERFACES.computeIfAbsent(query.getClass(), key ->
ClassUtils.getAllInterfacesForClass(key, this.proxyClassLoader));
result = Proxy.newProxyInstance(this.proxyClassLoader, ifcs,
new DeferredQueryInvocationHandler(query, target));
isNewEm = false;
Expand Down