|
43 | 43 | import org.springframework.util.Assert;
|
44 | 44 | import org.springframework.util.ClassUtils;
|
45 | 45 | import org.springframework.util.CollectionUtils;
|
| 46 | +import org.springframework.util.ConcurrentReferenceHashMap; |
46 | 47 |
|
47 | 48 | /**
|
48 | 49 | * Delegate for creating a variety of {@link javax.persistence.EntityManager}
|
|
65 | 66 | *
|
66 | 67 | * @author Juergen Hoeller
|
67 | 68 | * @author Rod Johnson
|
| 69 | + * @author Mark Paluch |
68 | 70 | * @since 2.0
|
69 | 71 | * @see javax.persistence.EntityManagerFactory#createEntityManager()
|
70 | 72 | * @see javax.persistence.PersistenceContextType#EXTENDED
|
|
73 | 75 | */
|
74 | 76 | public abstract class ExtendedEntityManagerCreator {
|
75 | 77 |
|
| 78 | + private static final Map<Class<?>, Class[]> CACHED_ENTITY_MANAGER_INTERFACES = new ConcurrentReferenceHashMap<>(); |
| 79 | + |
76 | 80 | /**
|
77 | 81 | * Create an application-managed extended EntityManager proxy.
|
78 | 82 | * @param rawEntityManager the raw EntityManager to decorate
|
@@ -222,17 +226,29 @@ private static EntityManager createProxy(
|
222 | 226 | boolean containerManaged, boolean synchronizedWithTransaction) {
|
223 | 227 |
|
224 | 228 | Assert.notNull(rawEm, "EntityManager must not be null");
|
225 |
| - Set<Class<?>> ifcs = new LinkedHashSet<>(); |
| 229 | + Class[] interfaces; |
| 230 | + |
226 | 231 | if (emIfc != null) {
|
227 |
| - ifcs.add(emIfc); |
| 232 | + interfaces = CACHED_ENTITY_MANAGER_INTERFACES.computeIfAbsent(emIfc, key -> { |
| 233 | + Set<Class<?>> ifcs = new LinkedHashSet<>(); |
| 234 | + ifcs.add(key); |
| 235 | + ifcs.add(EntityManagerProxy.class); |
| 236 | + return ClassUtils.toClassArray(ifcs); |
| 237 | + }); |
228 | 238 | }
|
229 | 239 | else {
|
230 |
| - ifcs.addAll(ClassUtils.getAllInterfacesForClassAsSet(rawEm.getClass(), cl)); |
| 240 | + interfaces = CACHED_ENTITY_MANAGER_INTERFACES.computeIfAbsent(rawEm.getClass(), key -> { |
| 241 | + Set<Class<?>> ifcs = new LinkedHashSet<>(); |
| 242 | + ifcs.addAll(ClassUtils |
| 243 | + .getAllInterfacesForClassAsSet(key, cl)); |
| 244 | + ifcs.add(EntityManagerProxy.class); |
| 245 | + return ClassUtils.toClassArray(ifcs); |
| 246 | + }); |
231 | 247 | }
|
232 |
| - ifcs.add(EntityManagerProxy.class); |
| 248 | + |
233 | 249 | return (EntityManager) Proxy.newProxyInstance(
|
234 | 250 | (cl != null ? cl : ExtendedEntityManagerCreator.class.getClassLoader()),
|
235 |
| - ClassUtils.toClassArray(ifcs), |
| 251 | + interfaces, |
236 | 252 | new ExtendedEntityManagerInvocationHandler(
|
237 | 253 | rawEm, exceptionTranslator, jta, containerManaged, synchronizedWithTransaction));
|
238 | 254 | }
|
|
0 commit comments