diff --git a/gemini/src/main/java/com/techempower/cache/CacheGroup.java b/gemini/src/main/java/com/techempower/cache/CacheGroup.java index 70f5d54..b522456 100755 --- a/gemini/src/main/java/com/techempower/cache/CacheGroup.java +++ b/gemini/src/main/java/com/techempower/cache/CacheGroup.java @@ -179,6 +179,9 @@ public void setObjects(Collection objects) /** * Resets this group, removing all the objects, and setting the initialized * flag to false. The group will be rebuilt from the database on next use. + * Can lead to temporary stale data in certain edge cases. See + * this issue + * for details. */ @Override public void reset() @@ -186,16 +189,28 @@ public void reset() synchronized (this) { setInitialized(false); - // Instantiate a new object so that any threads currently working with - // an old reference outside of a synchronized block will not be - // affected. - this.objects = new ConcurrentHashMap<>(); - this.objectsInOrder = new CopyOnWriteArrayList<>(); setErrorOnInitialize(false); resetHighLowIdentities(); } } + /** + * Synchronously resets and re-initializes this group, removing all the + * objects, and setting the initialized flag to false. The group will + * is then rebuilt from the database before the synchronization block + * ends. To avoids stale data in certain edge cases that {@link #reset()} + * can lead to. + */ + @Override + public void resetSynchronous() + { + synchronized (this) + { + reset(); + initializeIfNecessary(); + } + } + @Override public T get(long id) { @@ -1073,4 +1088,4 @@ public Builder constructorArgs(Object... arguments) } // End Builder. -} // End CacheGroup. \ No newline at end of file +} // End CacheGroup. diff --git a/gemini/src/main/java/com/techempower/data/EntityGroup.java b/gemini/src/main/java/com/techempower/data/EntityGroup.java index 2914d41..19e9a28 100755 --- a/gemini/src/main/java/com/techempower/data/EntityGroup.java +++ b/gemini/src/main/java/com/techempower/data/EntityGroup.java @@ -369,6 +369,17 @@ public void reset() // Does nothing here. } + /** + * Synchronously resets and re-initializes this group of entities. In + * the base class, this doesn't do anything, but subclasses such as + * CacheGroup act differently. + */ + public void resetSynchronous() + { + // Call reset() at least, for subclasses besides CacheGroup. + reset(); + } + /** * Returns the comparator to use when sorting entities of this type. */