JSP development-how to configure Level 2 cache in hibernate, jsphibernate

Source: Internet
Author: User

JSP development-how to configure Level 2 cache in hibernate, jsphibernate

How to configure level-2 cache in hibernate for JSP development

Hibernate second-level cache is also called process-level cache or SessionFactory-level cache.

The second-level cache is a global cache that can be shared by all sessions.

The lifecycle of the second-level cache is the same as that of SessionFactory. SessionFactory can manage the second-level cache.

The commonly used cache plug-in Hibernater second-level cache is a plug-in. Below are several frequently used cache plug-ins:

  • EhCache: It can be used as a process-range cache. The physical medium for storing data can be memory or hard disk, which provides support for Hibernate query cache.
  • OSCache:It can be used as a process-range cache. The physical medium for storing data can be memory or hard disk. It provides a wide range of cache data expiration policies and supports Hibernate's query cache.
  • SwarmCache: It can be used as a cache within the cluster, but does not support Hibernate query cache.
  • JBossCache: It can be used as a cache within the cluster. It supports transaction-type concurrent access policies and provides support for Hibernate query cache.

How to use second-level cache in a program:

Enable Level 2 cache in hibernate. cfg. xml

<Hibernate-configuration> <session-factory> ...... <! -- Enable Level 2 cache --> <property name = "hibernate. cache. use_second_level_cache"> true </property> <! -- Start "query cache". If you want to cache the data result set obtained by using methods such as findall (), list (), Iterator (), createCriteria (), and createQuery, this item must be configured --> <property name = "hibernate. cache. use_query_cache "> true </property> <! -- Set the Provider class of the level-2 Cache plug-in EHCache --> <! -- <Property name = "hibernate. cache. provider_class"> org. hibernate. cache. EhCacheProvider </property> --> <! -- Prefix of the second-level cache region name --> <! -- <Property name = "hibernate. cache. region_prefix"> test </property> --> <! -- High-speed cache provider --> <property name = "hibernate. cache. region. factory_class"> net. sf. ehcache. hibernate. EhCacheRegionFactory </property> <! -- Hibernate4 will be encapsulated in org. hibernate. cache. ehcache. EhCacheRegionFactory later --> <! -- Specify the cache configuration file location --> <! -- <Property name = "hibernate. cache. provider_configuration_file_resource_path"> ehcache. xml </property> --> <! -- Force Hibernate to store data in the second-level cache in a more user-friendly format --> <property name = "hibernate. cache. use_structured_entries"> true </property> <! -- Hibernate collects statistics that help with performance Adjustment --> <property name = "hibernate. generate_statistics "> true </property> ...... </session-factory> 

Then configure ehcache (ehcache. xml)

Detailed description of cache parameters:

● Name: Specifies the region name.
● MaxElementsInMemory: Maximum number of cached files in the memory
● MaxElementsOnDisk: Maximum number of cached Disks
● Eternal: Set whether to never expire
● OverflowToDisk: Number of Hard Disk overflows
● TimeToIdleSeconds: the maximum number of seconds after the object is idle
● TimeToLiveSeconds: the maximum number of seconds after the object is cached.
● MemoryStoreEvictionPolicy: cache algorithm, including LRU (default), LFU, and LFU

There are three common cache algorithms:

● LRU :( Least Rencently Used) Replace the recently Used object with the new object.
● LFU :( Least Frequently Used) replaces objects with low hit rates
● LFU :( First In First Out) Replace the First object that enters the second-level cache

Xml Code

<? Xml version = "1.0" encoding = "UTF-8"?> <Ehcache> <! -- If the number of OSS disks in the cache exceeds the specified value, --> <diskStore path = "D:/ehcache"/> <! -- Default cache: If no cache is available for a specific region, use the default cache --> <defaultCache maxElementsInMemory = "10000" eternal = "false" timeToIdleSeconds = "300" timeToLiveSeconds = "600" overflowToDisk = "false"/> <! -- Specify the region cache: Specify the region cache by name. The name corresponds to the region name in Hibernate. --> <cache name = "cn. javass. h3test. model. userModel "eternal =" false "maxElementsInMemory =" 100 "timeToIdleSeconds =" 1200 "timeToLiveSeconds =" 1200 "overflowToDisk =" false "> </cache> </ehcache>

Configure the cache element in the hbm file of each object. The usage can be read-only or read-write.

Xml Code

<Class> <! -- Set the level-2 Cache concurrent access policy of the persistence class read-only read-write nonstrict-read-write transactional --> <class name = "cn. java. test. model. user "table =" TBL_USER "> <cache usage =" read-write "/> ...... </class> 

The above are two implementation methods for hibernate second-level cache. There are still many articles on JSP hibernate development on this site. I hope you can find out more. Thank you for reading this article and hope it can help you, thank you for your support!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.