Skip to content

Commit 4be6231

Browse files
Thomas Darimontodrotbohm
authored andcommitted
DATAMONGO-1076 - Avoid resolving lazy-loading proxy for DBRefs during finalize.
We now handle intercepted finalize method invocations by not resolving the proxy. Previously the LazyLoadingProxy tried to resolve the proxy during finalization which could lead to unnecessary database accesses. Original pull request: spring-projects#234.
1 parent 4673e3d commit 4be6231

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DefaultDbRefResolver.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private boolean isLazyDbRef(MongoPersistentProperty property) {
178178
static class LazyLoadingInterceptor implements MethodInterceptor, org.springframework.cglib.proxy.MethodInterceptor,
179179
Serializable {
180180

181-
private static final Method INITIALIZE_METHOD, TO_DBREF_METHOD;
181+
private static final Method INITIALIZE_METHOD, TO_DBREF_METHOD, FINALIZE_METHOD;
182182

183183
private final DbRefResolverCallback callback;
184184
private final MongoPersistentProperty property;
@@ -192,6 +192,7 @@ static class LazyLoadingInterceptor implements MethodInterceptor, org.springfram
192192
try {
193193
INITIALIZE_METHOD = LazyLoadingProxy.class.getMethod("getTarget");
194194
TO_DBREF_METHOD = LazyLoadingProxy.class.getMethod("toDBRef");
195+
FINALIZE_METHOD = Object.class.getDeclaredMethod("finalize");
195196
} catch (Exception e) {
196197
throw new RuntimeException(e);
197198
}
@@ -255,6 +256,11 @@ public Object intercept(Object obj, Method method, Object[] args, MethodProxy pr
255256
if (ReflectionUtils.isHashCodeMethod(method)) {
256257
return proxyHashCode(proxy);
257258
}
259+
260+
// DATAMONGO-1076 - finalize methods should not trigger proxy initialization
261+
if (FINALIZE_METHOD.equals(method)) {
262+
return null;
263+
}
258264
}
259265

260266
Object target = ensureResolved();

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/convert/DbRefMappingMongoConverterUnitTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,26 @@ public void shouldNotEagerlyResolveIdPropertyWithPropertyAccess() {
541541
assertProxyIsResolved(proxy, false);
542542
}
543543

544+
/**
545+
* @see DATAMONGO-1076
546+
*/
547+
@Test
548+
public void shouldNotTriggerResolvingOfLazyLoadedProxyWhenFinalizeMethodIsInvoked() throws Exception {
549+
550+
MongoPersistentEntity<?> entity = mappingContext.getPersistentEntity(WithObjectMethodOverrideLazyDbRefs.class);
551+
MongoPersistentProperty property = entity.getPersistentProperty("dbRefToConcreteTypeWithPropertyAccess");
552+
553+
String idValue = new ObjectId().toString();
554+
DBRef dbRef = converter.toDBRef(new LazyDbRefTargetPropertyAccess(idValue), property);
555+
556+
WithObjectMethodOverrideLazyDbRefs result = converter.read(WithObjectMethodOverrideLazyDbRefs.class,
557+
new BasicDBObject("dbRefToPlainObject", dbRef));
558+
559+
ReflectionTestUtils.invokeMethod(result.dbRefToPlainObject, "finalize");
560+
561+
assertProxyIsResolved(result.dbRefToPlainObject, false);
562+
}
563+
544564
private Object transport(Object result) {
545565
return SerializationUtils.deserialize(SerializationUtils.serialize(result));
546566
}

0 commit comments

Comments
 (0)