6.7.2 Cache reference forms and cache types

There are three types of cache reference forms:

The cache types differ depending on which form is used to reference the cache. The following table describes the mapping of the cache reference forms and cache types.

Table 6-15 Mapping of the cache reference forms and cache types

Cache reference formsCache types
Hard referenceFull
Hard reference + Weak reference#1HardWeak
Weak reference + Soft reference#1SoftWeak
Weak referenceWeak
None#2None

#1 The reference forms are combined.

#2 The entity object is not cached.


With Cosminexus JPA Provider, you can choose the cache type. Choose the type based on the application design and environment. Specify the cache type in persistence.xml. For details on persistence.xml, see 6.2 persistence.xml in the uCosminexus Application Server Definition Reference Guide.

The following points describe the cache types.

Organization of this subsection
(1) Full
(2) HardWeak
(3) SoftWeak
(4) Weak
(5) NONE

(1) Full

All the entities are cached with a hard reference.

If you specify Full in the cache type, the access to the database decreases, so the processing load decreases. However, the memory continues to be occupied, therefore, the load on the memory increases.

Specify Full when the duration of the entity object is long and when the reference is created for a few entity objects that require frequent access. Furthermore, when several entity objects are read, the memory load increases, so we do not recommend using Full to update multiple records in a batch.

When Full is specified, the hard reference area is allocated with the specified cache size. If the hard reference area exceeds the defined size, the area is increased based on the Hashtable specifications. The following figure shows the image in the cache when Full is specified.

Figure 6-15 Cache image for Full

[Figure]

(2) HardWeak

The entities are cached with a hard reference and weak reference.

When you want to store the entity object in a list, use a hard reference. Create the hard reference area with a fixed length only up to the value specified in the cache size. If the cache size reaches the specified value, the old entity objects are moved to the weak reference. At this time, the entity objects for which cache registration has not been used for the longest time are moved sequentially to the weak reference. If the entity objects moved to the weak reference are used, the entity objects are once again stored in the hard reference area.

If you specify HardWeak, you can use the entity objects with a long duration to efficiently control the memory used in the cache.

If the state of insufficient memory occurs frequently in a system where SoftWeak is used, you cannot take advantage of the soft reference; therefore, use HardWeak. The following figure shows the image in the cache for HardWeak.

Figure 6-16 Cache image for HardWeak

[Figure]

In the case of HardWeak, the cache is stored with a hard reference in the hard reference cache area. Also, the cache is stored with a weak reference in the weak reference cache area.

(3) SoftWeak

The entities are cached with a soft reference and weak reference.

You use the soft reference to store the entity objects in a list and create a soft reference area of a fixed length only up to the value specified in the cache size. If the cache size reaches the specified value, the old entity objects are moved to the weak reference area. At this time, the entity objects for which cache registration has not been used for the longest time are moved sequentially to the weak reference. If the entity objects moved to the weak reference are used, the entity objects are once again stored in the hard reference area.

If you specify SoftWeak, you can use the entity objects with a long duration to efficiently control the memory used in the cache. Therefore, when you use the cache functionality, we recommend that you specify SoftWeak. The following figure shows the image in the cache for SoftWeak.

Figure 6-17 Cache image for SoftWeak

[Figure]

In the case of SoftWeak, the cache is stored with a soft reference in the soft reference cache area. Also, the cache is stored with a weak reference in the weak reference cache area.

(4) Weak

All the entities are cached with a weak reference.

Therefore, all the entity objects are subject to garbage collection. If you specify weak, the memory load decreases, but the cache might be deleted due to garbage collection. Use the Weak cache type in systems that do not place significance on the cache functionality of the entity objects. The following figure shows the image in the cache for weak.

Figure 6-18 Cache image for Weak

[Figure]

In the weak cache type, the entities are cached with a weak reference.

(5) NONE

No entity objects are cached. Use the None cache type if you want to destroy an entity object immediately after the entity object is read from the database.