Skip to content

Commit 4c97015

Browse files
Merge pull request #106 from ajohnstonTE/fix-cache-group-race-condition
Fix cache group race condition
2 parents 30d1442 + d75fb61 commit 4c97015

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

gemini/src/main/java/com/techempower/cache/CacheGroup.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -179,23 +179,38 @@ public void setObjects(Collection<T> objects)
179179
/**
180180
* Resets this group, removing all the objects, and setting the initialized
181181
* flag to false. The group will be rebuilt from the database on next use.
182+
* Can lead to temporary stale data in certain edge cases. See
183+
* <a href="https://github.com/TechEmpower/gemini/issues/105">this issue</a>
184+
* for details.
182185
*/
183186
@Override
184187
public void reset()
185188
{
186189
synchronized (this)
187190
{
188191
setInitialized(false);
189-
// Instantiate a new object so that any threads currently working with
190-
// an old reference outside of a synchronized block will not be
191-
// affected.
192-
this.objects = new ConcurrentHashMap<>();
193-
this.objectsInOrder = new CopyOnWriteArrayList<>();
194192
setErrorOnInitialize(false);
195193
resetHighLowIdentities();
196194
}
197195
}
198196

197+
/**
198+
* Synchronously resets and re-initializes this group, removing all the
199+
* objects, and setting the initialized flag to false. The group will
200+
* is then rebuilt from the database before the synchronization block
201+
* ends. To avoids stale data in certain edge cases that {@link #reset()}
202+
* can lead to.
203+
*/
204+
@Override
205+
public void resetSynchronous()
206+
{
207+
synchronized (this)
208+
{
209+
reset();
210+
initializeIfNecessary();
211+
}
212+
}
213+
199214
@Override
200215
public T get(long id)
201216
{
@@ -1073,4 +1088,4 @@ public Builder<T> constructorArgs(Object... arguments)
10731088

10741089
} // End Builder.
10751090

1076-
} // End CacheGroup.
1091+
} // End CacheGroup.

gemini/src/main/java/com/techempower/data/EntityGroup.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,17 @@ public void reset()
369369
// Does nothing here.
370370
}
371371

372+
/**
373+
* Synchronously resets and re-initializes this group of entities. In
374+
* the base class, this doesn't do anything, but subclasses such as
375+
* CacheGroup act differently.
376+
*/
377+
public void resetSynchronous()
378+
{
379+
// Call reset() at least, for subclasses besides CacheGroup.
380+
reset();
381+
}
382+
372383
/**
373384
* Returns the comparator to use when sorting entities of this type.
374385
*/

0 commit comments

Comments
 (0)