EclipseLink/UserGuide/JPA/Basic JPA Development/Caching/Query Options
|Mailing List • Forums • IRC|
Query Options and In-memory Querying
JPA defines standard query hints for configuring how a query interacts with the shared (L2) cache. EclipseLink also provides some additional query hints for configuring the cache usage.
Objects can be accessed through JPA using either find() or queries. find() will first check the persistence context cache (L1), and second check the shared persistence unit cache (L2), before accessing the database. By default all queries will access the database, unless by Id or 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 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 a flush() has written changes, or an update or native SQL query has been executed, then the shared cache will be ignored and objects will be built directly from the row data.
Configuring Query Results Cache
The query results cache is configured through query hints.
|eclipselink.query-results-cache||Enable query results cache for the named query.||no cached results||Required|
|eclipselink.query-results-cache.size||Sets the fixed size of the query results cache for the named query. The size is the number of queries with unique parameters to cache. If the query has no parameters, a value of 1 should be used.||100||Optional|
|eclipselink.query-results-cache.expiry||Sets the expiry time in milliseconds of the cached results.||no expiry||Optional|
|eclipselink.query-results-cache.expiry-time-of-day||Configures the time of day expiry. Valid values are string time format, "HH:MM:SS".||no expiry||Optional|
|eclipselink.query-results-cache.randomize-expiry||Set if the expiry time should be randomized by 10% of the set expiry time. This can be used to avoid a bottleneck on a fixed expiry time.||false||Optional|
|eclipselink.query-results-cache.type||Set the cache type. Valid values are defined in the CacheType enum. Normally an LRU fixed sized CACHE is used, a FULL, WEAK or SOFT cache can also be used.||CACHE||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.setParameter("firstName", "B%"); query.setParameter("lastName", "S%"); List<Employee> employees = query.getResultList();