Hibernate second-level cache

Source: Internet
Author: User

Hibernate second-level cache
Idea: (1) What is cache (2) Why does the hibernate session have a level-1 cache? (3) how to configure the second-level cache of Hibernate. (1) cache stores previously queried and used objects in the memory (a Data Structure). This data structure is usually or similar to HashMap, when you want to use an object in the future, first query whether the object exists in the cache. If so, use the object in the cache. If not, query the database, and save the queried objects in the cache for future use. The following is the cached pseudocode:

Dao{    HashMap map = new Map();    User getUser(Integer id){        User user = map.get(id);        if(user == null){            user = session.get(id);            map.put(id,user);        }        return user;    }}Dao{    Cache cache = null;    setCache(Cache cache){        this.cache = cache;    }         User getUser(int id){        if(cache != null){            User user = cache.get(id);            if(user == null){                user = session.get(id);                cache.put(id,user);            }            return user;        }        return session.get(id);    }}

 

(2) The Hibernate Session is a kind of cache, which is usually called the Hibernate first-level cache. When you want to use the session to query an object from the database, the session first checks whether the object exists from the internal server. If the object exists, the system returns the result directly without the internal server. Because a session represents a session, and a session is associated with a database connection, it is recommended that the session not be opened for a long time. Generally, it is only used in a transaction and closed at the end of the transaction. In addition, the session is insecure and shared by multiple threads is prone to problems. Generally, only the cache in the global sense is the real cache application, which has a great cache value. Therefore, the Hibernate session level cache function is not obvious and has little application value. Hibernate's second-level cache is to configure a global cache for Hibernate so that multiple threads and transactions can share the cache. (3) Level-2 cache is a software component independent of hibernate. It is a third-party product. Multiple Vendors and organizations provide cache products, such as EHCache and OSCache. To use the second-level cache in hibernate, you must first go to hibernate. cfg. in the xml configuration file, you need to configure the cache product's own configuration file, finally, you need to configure the Entity objects in hibernate to be included in the level-2 Cache Management. Extended Knowledge: A sessionfactory can be associated with a second-level cache, that is, a second-level cache can only cache data in a database. After hibernate's second-level cache is used, do not use other applications or sessionfactory to change the data in the current database, so that the cached data is inconsistent with the actual data in the database.

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.