9.7.1 Processing of the cache functionality
If you use the cache functionality, when the same entity is read, the data is obtained from the cache instead of the database. This section describes the procedure for processing the cache functionality, the cache registration and update timing, and the procedure for updating the cache.
- Organization of this subsection
(1) Procedure for processing the cache functionality
The following figure shows the flow of processing of the cache functionality.
|
A description of the above figure is as follows:
-
Reading of the entity
When an entity is first read with methods such as find, the flow of processing is as follows:
-
The entity is read.
-
The data is obtained from the database.
-
The entity object of the obtained data is registered in the cache.
-
When the entity has a relationship and when you obtain the relationship destination entity, if the target entity exists in the cache, the entity in the cache is referenced without accessing the database.
A cache exists for each persistence context.
(2) Cache registration and update timing
Registration and update in a cache is implemented at the following times:
-
Cache registration timing
During the reading of an entity object (find operation) when the target cache does not exist
-
Cache update timing
-
When the entity is refreshed (refresh operation)
-
When a transaction is committed
-
When an exception occurs in the optimistic lock processing
Note that when the OptimisticLockException exception occurs in the optimistic lock processing, the entity objects registered in the cache are deleted.
-
(3) Procedure for updating the cache
The following figure shows the flow of updating the cache.
|
A description of the figure is as follows:
-
The following operations are implemented for the entity object:
-
refresh operation
-
Transaction commit
-
-
During the refresh operation, the database is accessed.
-
The data existing in the persistence context is registered in the cache and the cache data is updated.
Note that the cache is registered even when JPQL is executed. The cache is registered if JPQL is executed when the target cache does not exist. The cache data is used in JPQL as well, but the database is accessed irrespective of the presence or absence of the cache data. Therefore, the cache-based processing performance cannot be expected to improve. For details, see point (4).
The cache stores information for the entity objects; therefore, if the object returned during the execution of the query is the entity itself, the cache is updated. The cache is not updated when other fields are specified. The JPQL examples when the cache update is valid and when the update is invalid are as follows:
-
JPQL example when the cache is updated
SELECT emp FROM Employee AS emp
-
JPQL example when the cache is not updated
SELECT emp.id, emp.name, emp.address FROM Employee AS emp
(4) Relation between JPQL and cache
When all the entity objects are obtained with JPQL, if there is information registered in the cache, the cache information is obtained. The cache functionality of Cosminexus JPA Provider requires a primary key that is the ID for identifying the target entity. To obtain the primary key, the JPQL result must be obtained and at this time, the database is accessed. The primary key is extracted from the database access results and the entity object is obtained from the cache. Also, if the target data does not exist in the cache, the entity is created from the database information.
With JPQL, the database is accessed regardless of the presence or absence of the cache data. Therefore, for JPQL, cache-based performance improvement cannot be expected.