1
votes

Currently using eclipselink JPA provider for accessing backend entities. I'm using namedqueries for accessing data and using the below options on query caching.

@NamedQueries({
@NamedQuery(name = Supplier.FIND_ALL, query = "select o from Supplier o",hints={
      @QueryHint(name=QueryHints.READ_ONLY, value=HintValues.TRUE),          
      @QueryHint(name = QueryHints.QUERY_RESULTS_CACHE, value = HintValues.TRUE),
      @QueryHint(name = QueryHints.CACHE_STATMENT, value = HintValues.TRUE),
      @QueryHint(name = QueryHints.CACHE_STORE_MODE, value = "REFRESH"),
      @QueryHint(name = QueryHints.CACHE_RETRIEVE_MODE,value=CacheUsage.CheckCacheThenDatabase),
      }),
})

Also i'm using the below Cache options on the entity as well.

@Cache(refreshOnlyIfNewer=true,
coordinationType=CacheCoordinationType.SEND_OBJECT_CHANGES,alwaysRefresh=true)

The query seems to be taking sometime for the first time, but is pretty fast for further retrievals ( b'cozs of QueryHints.QUERY_RESULTS_CACHE, value = HintValues.TRUE ). But it seems any changes to the database subsequently, are not getting reflected in the output. It seems the cache is not getting refreshed and updated changes to the database are not reflected in the output.

Require help on the same.

Thanks, Krishna

1

1 Answers

2
votes

You settings don't make a lot of sense.

QueryHints.QUERY_RESULTS_CACHE is the correct way to enable the query cache. But you should not set the others,

QueryHints.CACHE_STATMENT - this is JDBC statement caching, very odd to be setting this on a query, normally this is configured for all statements in the DatabaseSource, or persistence unit config if using EclipseLink connection pooling.

QueryHints.CACHE_STORE_MODE - I'm not sure this makes sense, you cannot refresh and cache the query.

QueryHints.CACHE_RETRIEVE_MODE,value=CacheUsage.CheckCacheThenDatabase, this makes no sense, CacheUsage is not for this property, and CheckCacheThenDatabase makes no sense for a query that is using a query cache.

EclipseLink has two types of caches, the "object cache" (by Id) and the "query cache" (by query name and parameters). When you enable a query cache, the query results will be cached until they expire. By default there is no expiry, but you can configure it, or manually clear the query cache. The query cache will not be updated with database changes.

See, http://wiki.eclipse.org/EclipseLink/UserGuide/JPA/Basic_JPA_Development/Caching/Query_Cache