1

I want an in-time got objects from database. I decided not to permit caching of my entities. By using this configuration:

@Cache(
        type = CacheType.NONE/*,
        alwaysRefresh = true,
        disableHits = true,
        coordinationType = INVALIDATE_CHANGED_OBJECTS*/
)

But I have read the documentation here that:

NONE

public static final CacheType NONE
WARNING: Does not preserve object identity and does not cache objects.

Is that warning important, I may understand that JVM may misrelate objects to their real identities! Is there any suggestion for having the best configuration of not caching an object like playing with alwaysRefresh for example.

Neil Stockton
  • 11,383
  • 3
  • 34
  • 29
Houssam Badri
  • 2,441
  • 3
  • 29
  • 60

1 Answers1

2

If your program is the only application that uses the database you should not disable caching, if there are multiple clients it may be correct to disable caching, or reconfigure caching.

As with any caching strategy the tradeoff is between speed and stale data. Most JPA implementations share a (2nd level) cache between the Persistence Contexts (the PC can be though of a 1st level cache), so when you load an entity using em.find() there is no need to access the database if the instance is already cached. If another client has access to the database you may end up serving stale data if the database was modified.

If your have a clustered JPA application, and the database is only accessed by your JPA applications, you can still use caching, as long as you configure cache-coordination. When cache-coordination is used, one instance will inform the others if an entity was updated, so the other instances can update their cache, or discard their cached version.

In the solutions I have built I have almost always had to disable caching, because the database has multiple clients, and we never wanted to display stale data.

Klaus Groenbaek
  • 4,820
  • 2
  • 15
  • 30
  • Thanks a lot. In fact my application is used by many client over a LAN. So, I decided to type = CacheType to SOFT (to not lose identities) and alwaysRefresh to true. If you agree just make some update to your answer to make it the correct one – Houssam Badri Jan 11 '17 at 09:44
  • I typically configure it globally on the JPA provider, If you are using a persistence.xml you can add NONE . Normally I use EclipseLink in a code-only approach so it set ```jpaProperties.setProperty(org.eclipse.persistence.config.PersistenceUnitProperties.CACHE_SHARED_DEFAULT, "false")```. – Klaus Groenbaek Jan 11 '17 at 10:08
  • Setting caching to nono will lead to entities' references problems. This one has some random problems like i faced: http://stackoverflow.com/questions/20920400/jpa-2-eclipselink-caching-issue?lq=1 – Houssam Badri Jan 12 '17 at 06:43
  • I have never had any problems when disabling the shared (intra persistance context) cache, and I have used Eclipselink in every project since it was created (and before that I used Toplink). Most JPA problems arise from the fact that developers don't know how an Entitymanager works and how, and that in some cases it is different from a persistence context. The first level cache is in the persistence context not the entity manager, but you interact with the persistence context through the Entitymanager. – Klaus Groenbaek Jan 12 '17 at 08:56