Skip to content

Commit e163a69

Browse files
mp911deodrotbohm
authored andcommitted
DATACMNS-1364 - Store persistent properties in ConcurrentHashMap.
We now use ConcurrentHashMap to store persistent properties of a PersistentEntity and to prevent eviction caused by GC activity. Previously, we used ConcurrentReferenceHashMap defaulting to soft references. Soft references can be cleared at the discretion of the GC in response to memory demand. So a default ConcurrentReferenceHashMap is memory-sensitive and acts like a cache with memory-based eviction rules. Persistent properties are not subject to be cached but elements of a PersistentEntity and cannot be recovered once cleared. Original pull request: #304.
1 parent b80f5c6 commit e163a69

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ public BasicPersistentEntity(TypeInformation<T> information, @Nullable Comparato
9494
this.constructor = PreferredConstructorDiscoverer.discover(this);
9595
this.associations = comparator == null ? new HashSet<>() : new TreeSet<>(new AssociationComparator<>(comparator));
9696

97-
this.propertyCache = new HashMap<>();
98-
this.annotationCache = new HashMap<>();
99-
this.propertyAnnotationCache = new LinkedMultiValueMap<>();
97+
this.propertyCache = new ConcurrentHashMap<>();
98+
this.annotationCache = new ConcurrentReferenceHashMap<>();
99+
this.propertyAnnotationCache = CollectionUtils.toMultiValueMap(new ConcurrentReferenceHashMap<>());
100100
this.propertyAccessorFactory = BeanWrapperPropertyAccessorFactory.INSTANCE;
101101
this.typeAlias = Lazy.of(() -> getAliasFromAnnotation(getType()));
102102
}

src/test/java/org/springframework/data/mapping/model/BasicPersistentEntityUnitTests.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -126,24 +126,27 @@ public void considersComparatorForPropertyOrder() {
126126
assertThat(entity.getPersistentProperty("ssn")).isEqualTo(iterator.next());
127127
}
128128

129-
@Test // DATACMNS-186
129+
@Test // DATACMNS-18, DATACMNS-1364
130130
public void addingAndIdPropertySetsIdPropertyInternally() {
131131

132132
MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
133133
assertThat(entity.getIdProperty()).isNull();
134134

135+
when(property.getName()).thenReturn("id");
135136
when(property.isIdProperty()).thenReturn(true);
136137
entity.addPersistentProperty(property);
137138
assertThat(entity.getIdProperty()).isEqualTo(property);
138139
}
139140

140-
@Test // DATACMNS-186
141+
@Test // DATACMNS-186, DATACMNS-1364
141142
public void rejectsIdPropertyIfAlreadySet() {
142143

143144
MutablePersistentEntity<Person, T> entity = createEntity(Person.class);
144145

146+
when(property.getName()).thenReturn("id");
145147
when(property.isIdProperty()).thenReturn(true);
146148
when(anotherProperty.isIdProperty()).thenReturn(true);
149+
when(anotherProperty.getName()).thenReturn("another");
147150

148151
entity.addPersistentProperty(property);
149152
exception.expect(MappingException.class);
@@ -331,7 +334,7 @@ static class PersistableEntity implements Persistable<Long> {
331334

332335
private final Long id = 42L;
333336

334-
/*
337+
/*
335338
* (non-Javadoc)
336339
* @see org.springframework.data.domain.Persistable#getId()
337340
*/
@@ -340,7 +343,7 @@ public Long getId() {
340343
return 4711L;
341344
}
342345

343-
/*
346+
/*
344347
* (non-Javadoc)
345348
* @see org.springframework.data.domain.Persistable#isNew()
346349
*/

0 commit comments

Comments
 (0)