3

The javadocs for CacheStoreMode differentiate in a point I cannot really grasp:

The javadocs for the USE mode:

Insert/update entity data into cache when read from database and when committed into database: this is the default behavior. Does not force refresh of already cached items when reading from database.

The javadocs for the REFRESH mode differ in the last sentence:

Forces refresh of cache for items read from database.

When an existing cached entity instance is updated when reading from database, this would typically involve overwriting the existing data. So what is the difference between forcing and not forcing a refresh in this context?

Thank you.

kostja
  • 60,521
  • 48
  • 179
  • 224

2 Answers2

2

As far as I know:

  • CacheStoreMode.USE should be used if a given EntityManagerFactory has an exclusive write-access to the underlying database thus it implies that there is no chance for an entity instance stored in the shared cache to be stale.
  • CacheStoreMode.REFRESH should be enabled if the underlying database might be accessed by multiple commiters (i.e. EntityManagerFactory instances, applications in different JVMs, external JDBC sources) thus an entity instance stored in the shared cache may become stale.

Since CacheStoreMode.USE does not force refresh of already cached entities when reading from the database, CacheStoreMode.REFRESH does.

wypieprz
  • 7,981
  • 4
  • 43
  • 46
0

I think it will make difference where the most recent updated data from the database is needed, where it gets updated from the back-end, not through the application.

In my application, its the same case (but not using any cache strategy), where we have to load all data each time; as it gets modified implicitly through messaging from the external system, else we will be dealing with the stale data.

There might be few cases where scheduled jobs, external systems etc. update database directly there CacheStoreMode.REFRESH is appropriate; while for normal case CacheStoreMode.USE.

[Other than this I can't recollect any other cases, where it might make difference between these two modes]


Edit: The documentation seems confusing & too short to explain properly. Also, in case of native queries, bulk updates etc. items are skipped & aren't cached.

CacheStoreMode.USE: Only new items are put into the cache, not for already cached ones. CacheStoreMode.REFRESH: New items are put into the cache & already existing cached items are refreshed.

Nayan Wadekar
  • 11,444
  • 4
  • 50
  • 73
  • Thanks for your answer, Nayan. I'm afraid I'm still not getting the difference :) The `USE` mode updates data when it is read from the DB as well as `REFRESH`, the difference is supposed to be the **forcing** of an update. What state does the persistence provider check against what and when is the state in the shared cache not updated, because it would be 'forcing'? – kostja May 06 '14 at 09:43
  • @kostja Now I too not getting you & confused :) I think the objects are directly replaced based on object identity, not updated. I have updated post, refer edit part. – Nayan Wadekar May 06 '14 at 10:24