(i) The general process of Hibernate's level two caching strategy is as follows:
1 when the condition is queried, always issue a SELECT * FROM table_name where .... (select all fields) such SQL statements query the database and get all the data objects at once.
2 All the data objects obtained are put into the second level cache according to the ID.
3 when hibernate according to ID access to data objects, first from the session-level cache, check, if configured with a level two cache, then from the level two cache, check, and then query the database, the results in accordance with the ID into the cache.
4 when deleting, updating, and adding data, update the cache at the same time.
Hibernate's level Two caching strategy is for the cache policy for the ID query, and it has no effect on the conditional query. To do this, Hibernate provides query Cache for conditional queries.
(ii) What data is suitable for storage in the secondary cache?
1 data that is rarely modified
2 is not very important data, allowing occasional concurrent data to occur
3 data that will not be accessed concurrently
4 reference data, refers to the supply of reference to the constant data, its number of instances are limited, its instances are referenced by many other classes, the instance is very few or never be modified.
(iii) data not suitable for storage to the second level of caching?
1 frequently modified data
2 financial data, absolutely not allowed to appear concurrent
3 data that is shared with other applications.
Practice section:
To configure level two caching using Ehcache:
Configuration preparation:
1) to add Ehcache-1.2.3.jar to the current application of the classpath.
2 Add the Ehcache cache plug-in to the Hibernate.cfg.xml file.
<!--配置缓存插件 -->
<property name="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</property>