Hibernate level 1 and level 2 cache (2)

Source: Internet
Author: User

Hibernate cache category:

Session cache, level 1 cache;

Sessionfactory caches are divided into built-in caches and external caches. the built-in cache stores data contained in some collection attributes of the sessionfactory object (ing element data and predefined SQL statements). For applications, it is read-only. the external cache stores copies of database data, which is similar to the primary cache. in addition to using memory as the storage medium, secondary cache can also use external storage devices such as hard disks.

Hibernate cache range:

Both the level-1 cache and level-2 cache of Hibernate are on the persistent layer and are used to store copies of database data. The biggest difference is that the cache range is different.

The cache scope is divided into three categories:

1. Transaction Scope
The cache of the transaction scope can only be accessed by the current transaction. Each transaction has its own cache, and the data in the cache is usually in the form of correlated objects. the cache lifecycle depends on the transaction lifecycle. The cache lifecycle ends only when the transaction ends. the cache within the transaction scope uses the memory as the storage medium, and the first-level cache is within the transaction scope.
2. Application Scope
The application cache can be accessed by all transactions within the application scope. the cache lifecycle depends on the application lifecycle. The cache lifecycle ends only when the application ends. the memory or hard disk can be used as the storage medium for application cache, and the second-level cache belongs to the application scope.
3. Cluster Scope
In the cluster environment, the cache is shared by processes of one or more machines, and the cached data is copied to each process node in the cluster environment, processes communicate remotely to ensure data consistency in the cache. Data in the cache is usually in the form of loose data of objects.

Hibernate Cache Management

Level 1 Cache Management:

Evit (Object OBJ) clears the specified persistent object from the first-level cache, releases the memory resources occupied by the object, and changes the object from the persistent state to the out-of-the-tube state to the free object.
Clear () clears all persistent objects in the first-level cache and releases the memory resources it occupies.
Contains (Object OBJ) determines whether the specified object exists in the first-level cache.
Flush () refreshes the content of the level-1 cache so that it can be synchronized with the database data.

Second-level Cache Management:
Evict (class arg0, serializable arg1) clears the persistence object of the specified ID of a class from the second-level cache and releases the resources occupied by the object.
Java code: sessionfactory. evict (customer. Class, new INTEGER (1 ));

Evict (class arg0) clears all persistent objects of the specified class from the second-level cache and releases the memory resources it occupies.
Java code: sessionfactory. evict (customer. Class );

Evictcollection (string arg0) clears the specified set of all persistent objects of the specified class from the second-level cache to release the memory resources it occupies.
Java code: sessionfactory. evictcollection ("customer. Orders ");

Hibernate second-level cache Configuration
First, not all data is suitable for the second-level cache. Let's take a look at what data is suitable for the second-level cache? What kind of data is not suitable for second-level cache?

The following situations are not suitable for loading to the second-level cache:
1. frequently modified data
2. concurrent access to data is absolutely not allowed
3. data shared with other applications

 

The following situations are suitable for loading to the second-level cache:
1. Low data update frequency
2. Allow occasional concurrent errors of non-important data
3. data that will not be accessed concurrently
4. Constant data
5. Data not modified by a third party

 

Hibernate's second-level cache function is implemented by configuring second-level cache plug-ins. To integrate these plug-ins, Hibernate provides Org. hibernate. cache. cacheprovider acts as an adapter between the cache plug-in and hibernate.

Common second-level cache plug-ins
Ehcache org. hibernate. cache. ehcacheprovider
Oscache org. hibernate. cache. oscacheprovider
Swarmcahe org. hibernate. cache. swarmcacheprovider
Jbosscache org. hibernate. cache. treecacheprovider

Brief Introduction to ehcache Configuration
Hibernate. cfg. xml

<Hibernate-config> <br/> <session-factory> <br/> <! -- Set the provider class of the second-level cache plug-in ehcache --> <br/> <property name = "hibernate. cache. provider_class "> <br/> Org. hibernate. catch. ehcacheprovider <br/> </property> <br/> <! -- Start "query cache" --> <br/> <property name = "hibernate. cache. use_query_cache "> <br/> true <br/> </property> <br/> </session-factory> <br/> </pibernate-config>

Ehcache. xml

 <Ehcache> <br/> <! -- Maxelementsinmemory is the maximum number of cached objects. If eternal is set to never expire, the maximum number of seconds for timetoidleseconds objects to be idle is displayed, the maximum number of seconds for timetoliveseconds objects to be cached --> <br/> <diskstore Path = "Java. io. tmpdir "/> <br/> <defaultcache maxelementsinmemory =" 10000 "Eternal =" false "timetoidleseconds =" 300 "timetoliveseconds =" 600 "overflowtodisk =" true "/> <br/> </ehcache>

 

* ***. HBM. xml
<? XML version = "1.0" encoding = 'utf-8'?> <Br/> <! Doctype hibernate-mapping Public <br/> "-// hibernate/hibernate DTD ing DTD 3.0/EN" <br/> "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> </P> <p> <pibernate -Mapping> <br/> <class> <br/> <! -- Set the level-2 Cache concurrent access policy of the persistence class read-only read-write nonstrict-read-write transactional --> <br/> <Cache Usage = "read-write"/> <br/> </class> </P> <p> </pibernate-mapping>

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.