Hibernate first-level caching and snapshots

Source: Internet
Author: User

excerpt from the network:       The first level cache in Hibernate is in the form of a map, the key is the primary key, and value is the object, so its generics are map<serializable,object>, The generics of key are serializable because the primary key can be serialized.
Simply finish the underlying principle of the first level cache, and start with the snapshot problem. When the program calls the Get () method, the session will first in the buffer to find whether there is a corresponding object, if it is the first call, then the buffer is empty, naturally can not find, then the session will send a SELECT statement to the database to find
, the data is found after the results are returned to the session, then the session will assemble the data into a physical object, and a two copies, that is, one copy in the cache, one is a snapshot, then the session will be cached in the copy back to the program;
If an update is performed at this time, the object in the cache is compared to the object in the snapshot in the session, and if the object's properties are changed, an UPDATE statement is sent that updates the database, if the object's properties do not change.
The UPDATE statement is not sent. In summary, a snapshot is a representation of the database's data in the session, that is, once the data in the database has changed, the snapshot changes. Personally, this design can reduce access to the database and improve the efficiency of the database.
Hibernate has 2 caches the first level of cache is Session-level cache, which is a transaction-scoped cache (session-level cache) The second level of cache is the sessionfactory level of cache, which is a process-wide cache (level two cache Session of the first-level cache when using Hibernate query, the query results placed in the session of the first-level cache, in the first-level cache of objects, objects using the attributes of the OID value to distinguish, at this time using the same OID to query the time,
First, in the session cache to find if there is the same OID, if there is the same OID, no longer query the database, but directly use the first-level cache in the presence of objects if there is no same OID, then query the database, The result data of the query is then placed in the session cache. Objective: To reduce the number of times the database is accessed the session's snapshot uses the ID to query the database, placing the results of the query into the session-level cache, and copying a copy of the data. Placed in a snapshot of session when using Tr.commit (), the first level cache (flush) of the session is cleared when the session cache is cleared, the OID is used to determine the object in the primary cache and the object in the snapshot. If the properties in 2 objects (cache-level objects and snapshot objects) change, the UPDATE statement is executed, updating the database, updating the data in the first-level cache if the properties in the 2 objects do not change, the UPDATE statement is not performed objective: ensure consistency with data in the database Session level Two caches a configurable cache plug-in that includes class-level cache areas, collection-level cache areas, query-level cache areas, and timestamp-level cache areas that use steps roughly like Below:1Copy ehcache-1.5.0. Jar into the current project's lib directory2turn on level two caching< PropertyName="Hibernate.cache.use_second_level_cache ">true</property>3to specify the vendor for the cache< PropertyName="Hibernate.cache.provider_class ">Org.hibernate.cache.EhCacheProvider</ Property>4specifies a class method that uses a level two cache that uses the class's*The . Hbm.xml configuration selects a persistence class that requires a level two cache and sets its level two cache concurrency access policy.<classThe cache child element of the > element indicates that Hibernate caches the simple properties of the object, but does not cache the collection properties.
If you want to cache the elements in a collection property, you must include the <set> element in the <cache>child element method Two is configured in the Hibernate.cfg.xml file (recommended)<!--specify classes that use level two caching to be placed under maping-<!--Configure class-level two cache--<class-cacheclass="Cn.itcast.c3p0.Customer "usage=" Read-write "/><class-cacheclass="cn.itcast.c3p0.Order "usage=" Read-write "/><!--Configure a collection-level two cache--<collection-cache collection="cn.itcast.c3p0.Customer.orders "usage="read-write "/>5Configure Ehcache Default profile ehcache.xml (fixed name) (placed under Classpath) hibernate in first-level cache, snapshot, two-level cache the interview was a lot more time to ask.

Hibernate first-level caching and snapshots

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.