Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Query Options"

(EclipseLink Cache Query Hints)
(In-memory query example)
Line 115: Line 115:
 
Query query = em.createQuery("Select e from Employee e where e.firstName like :firstName and e.lastName like :lastName");
 
Query query = em.createQuery("Select e from Employee e where e.firstName like :firstName and e.lastName like :lastName");
 
query.setHint("eclipselink.cache-usage", "CheckCacheThenDatabase");
 
query.setHint("eclipselink.cache-usage", "CheckCacheThenDatabase");
query.setParameter("firstName", "B%");
+
query.setParameter("firstName", "Bob");
query.setParameter("lastName", "S%");
+
query.setParameter("lastName", "Smith");
 
List<Employee> employees = query.getResultList();
 
List<Employee> employees = query.getResultList();
 
</source>
 
</source>

Revision as of 11:24, 30 May 2012

EclipseLink JPA

Eclipselink-logo.gif
EclipseLink
Website
Download
Community
Mailing ListForumsIRCmattermost
Issues
OpenHelp WantedBug Day
Contribute
Browse Source

Elug api package icon.png Key API

Query Cache Options and In-memory Querying

JPA defines standard query hints for configuring how a query interacts with the shared persistence unit cache (L2). EclipseLink also provides some additional query hints for configuring the cache usage.

Entities can be accessed through JPA using either find() or queries. find() will first check the persistence context cache (L1) for the Id, if the object is not found it will check the shared persistence unit cache (L2), if the object is still not found it will access the database. By default all queries will access the database, unless querying by Id or by cache indexed fields. Once the query retrieves the rows from the database, it will resolve each row with the cache. If the object is already in the cache, then the row will be discarded, and the object will be used. If the object is not in the shared cache, then it will be built from the row and put into the shared cache. A copy will also be put in the persistence context cache and returned as the query result.

This is the general process, but it differs if the transaction is dirty. If the transaction is diry then the shared persistence unit cache will be ignored and objects will be built directly into the persistence context cache.

A transaction is considered dirty in the following circumstances:

  • A flush() has written changes to the database.
  • A pessimistic lock query has been executed.
  • An update or delete query has been executed.
  • A native SQL query has been executed.
  • This persistence unit property "eclipselink.transaction.join-existing" is used.
  • The JDBC connection has been unwrapped from the EntityManager.
  • The UnitOfWork API beginEarlyTransaction has been called.

Entities can also be configured to be isolated, or non cacheable, in which case they will never be placed in the shared cache (see Shared, Isolated, Protected, Weak and Read-only Cache).

JPA Cache Query Hints

JPA 2.0 defines the following query hints to configure a queries interaction with the shared cache. JPA cache query hints can be set on named or dynamic queries, or set in the properties map passed to the find() operation.

JPA Cache Query Hints
Hint Description Default Required?
javax.persistence.cache.retrieveMode Configure how the shared cache is accessed.

Valid values are defined in CacheRetrieveMode enum:

  • BYPASS : Ignore the shared cache, and build the object directly into the persistence context cache.
  • USE : Use the shared cache. If the object is already in the shared cache, use the object and discard the database row data.
USE Optional
javax.persistence.cache.storeMode Configure how the shared cache is modified.

Valid values are defined in CacheStoreMode enum:

  • BYPASS : Do not put any object build from the row data into the shared cache.
  • REFRESH : If the object is in the shared cache, refresh its data from the database row data.
  • USE : If the object is not in the shared cache, then build it from the database row data and put it into the shared cache.
USE Optional
Refresh query hint example
Query query = em.createQuery("Select e from Employee e where e.address.city = :city");
query.setHint("javax.persistence.cache.storeMode", "REFRESH");
query.setParameter("city", "Ottawa");
List<Employee> employees = query.getResultList();

EclipseLink Cache Query Hints

EclipseLink defines the following query hints to configure a queries interaction with the shared cache:

EclipseLink Cache Query Hints
Hint Description Default Required?
eclipselink.refresh Configure the query to refresh the objects in both the shared cache and/or the persistence context cache with the database row data. false Optional
eclipselink.refresh.cascade Configures how refreshing is cascaded to relationships, if the query has been configured to refresh.

Valid values are defined in CascadePolicy:

  • NoCascading - Do not cascade the refresh to relationships.
  • CascadePrivateParts - Only cascade the refresh to private (orphan-removal) relationships.
  • CascadeAllParts - Cascade the refresh to all relationships.
  • CascadeByMapping - Only cascade the refresh to all relationships that have been configure to cascade refresh.
CascadeByMapping Optional
eclipselink.cache-usage Configures in-memory querying and cache usage.

Valid values are defined in CacheUsage:

  • DoNotCheckCache - Do not check the cache before accessing the database. The cache is still used to resolve rows.
  • CheckCacheByExactPrimaryKey - Only check the cache if the query is on only the Id or cache indexed fields.
  • CheckCacheByPrimaryKey - Check the cache if the query is on any Id or cache index field. Conform the query in memory to ensure the object matches the query criteria.
  • CheckCacheThenDatabase - Check the cache first before accessing the database. If any object is found in the cache matching the query criteria then return it. Only for use with single result queries.
  • CheckCacheOnly - Only check the cache and do not access the database. Return all objects found in the cache that match the query criteria. Note that not all queries can be conformed in memory.
  • ConformResultsInUnitOfWork - Access the database, but conform the results with an un-flushed in-memory changes made in the persistence context. Include any un-flushed new objects and exclude and un-flushed removed objects.
  • NoCache - For update and delete queries only. Do not invalidate the cache for the queries modified data.
  • Invalidate - For update and delete queries only. Invalidate the cache for the queries modified data (default).
CheckCacheByExactPrimaryKey Optional
In-memory query example
Query query = em.createQuery("Select e from Employee e where e.firstName like :firstName and e.lastName like :lastName");
query.setHint("eclipselink.cache-usage", "CheckCacheThenDatabase");
query.setParameter("firstName", "Bob");
query.setParameter("lastName", "Smith");
List<Employee> employees = query.getResultList();

Eclipselink-logo.gif
Version: 2.4 DRAFT
Other versions...

Copyright © Eclipse Foundation, Inc. All Rights Reserved.