JDO doesn't currently define a mechanism for caching of queries. DataNucleus provides 3 levels of caching
This cache is by default set to soft, meaning that the generic query compilation is cached using soft references. This is set using the persistence property datanucleus.cache.queryCompilation.type. You can also set it to strong meaning that strong references are used, or weak meaning that weak references are used, or finally to none meaning that there is no caching of generic query compilation information
You can turn caching on/off (default = on) on a query-by-query basis by specifying the query extension datanucleus.query.compilation.cached as true/false.
query.addExtension("datanucleus.query.compilation.cached", "true");
This cache is by default set to soft, meaning that the datastore query compilation is cached using soft references. This is set using the persistence property datanucleus.cache.queryCompilationDatastore.type. You can also set it to strong meaning that strong references are used, or weak meaning that weak references are used, or finally to none meaning that there is no caching of datastore-specific query compilation information
You can turn caching on/off (default = on) on a query-by-query basis by specifying the query extension datanucleus.query.compilation.cached as true/false.
query.addExtension("datanucleus.query.compilation.cached", "true");
This cache is by default set to soft, meaning that the datastore query results are cached using soft references. This is set using the persistence property datanucleus.cache.queryResult.type. You can also set it to strong meaning that strong references are used, or weak meaning that weak references are used, or finally to none meaning that there is no caching of query results information. You can also specify datanucleus.cache.queryResult.cacheName to define the name of the cache used for the query results cache.
You can turn caching on/off (default = off) on a query-by-query basis by specifying the query extension datanucleus.query.results.cached as true/false. As a finer degree of control, where cached results are used, you can omit the validation of object existence in the datastore by setting the query extension datanucleus.query.resultCache.validateObjects.
query.addExtension("datanucleus.query.results.cached", "true"); query.addExtension("datanucleus.query.resultCache.validateObjects", "false");
Obviously with a cache of query results, you don't necessarily want to retain this cached over a long period. In this situation you can evict results from the cache like this.
import org.datanucleus.api.jdo.JDOQueryCache; import org.datanucleus.api.jdo.JDOPersistenceManagerFactory; ... JDOQueryCache cache = ((JDOPersistenceManagerFactory)pmf).getQueryCache(); cache.evict(query);
which evicts the results of the specific query. The JDOQueryCache has more options available
should you need them ...
.