1. Why Hibernate caching?
Hibernate is a persistent layer framework that accesses physical databases frequently.
Improve application performance by reducing the frequency of application access to physical data sources.
The data in the cache is a copy of the data in the physical data source, the application reads and writes data from the cache at run time, and at a particular moment or event synchronizes the cache and the data from the physical data source.
Back to Top
2. Project Combat
When the session object invokes the Save () method to save an object, the object is placed in the session cache.
When the session object calls the Get () or load () method to fetch an object from the database, the object is also placed in the session cache.
When querying data from a database, such as writing HQL and QBC using the same Session, the data is read directly from the cache and the database is not accessed.
Hibernate offers several methods (Evit/clear/contains/flush ...). To manage and judge the cache level.
Now Java EE Dao layer, provided to the external database access, each time from the session factory to obtain a new session thread, so that the first-level cache is seldom exploited.
Example Project Source: Https://git.oschina.net/LanboEx/hiberdemo
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
1.Hibernate of its own first-level cache, you can see only output a SQL session session = GetSession (); Userpo Userpo = Session.get (Userpo.class, "031e7a36972e11e6acede16e8241c0fe"); System.out.println ("1. First time Access DB: "+ userpo.getname () +", "+ userpo.getpasswd ()); Userpo userPO1 = Session.get (Userpo.class, "031e7a36972e11e6acede16e8241c0fe"); System.out.println ("2. Second access DB: "+ userpo1.getname () +", first-level cache for specific objects "+ session.contains (Userpo));
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/362169/201612/362169-20161214160628714-1447812379. PNG "style=" margin:0px;padding:0px;border:0px; "/>
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
       //2. Use the evite/clear method to manually clear specific objects in the cache and see hiber output of two strips SQL Session session1 = GetSession (); userpo userpo3 = session1.get ( userpo.class, "031e7a36972e11e6acede16e8241c0fe"); System.out.println ("3. First Get object:" + userpo3.getname () + "," + USERPO3.GETPASSWD ()); session1.evict (userPO3); userpo userpo4 = session1.get (UserPO.class, " 031e7a36972e11e6acede16e8241c0fe "); system.out.println (" 4. Second Fetch object: " + userpo4.getname () + ", " + userpo4.getpasswd ());
650) this.width=650; "src="/img/fz.gif "alt=" Copy Code "style=" Margin:0px;padding:0px;border:none; "/>
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/362169/201612/362169-20161214160636573-1709868765. PNG "style=" margin:0px;padding:0px;border:0px; "/>
Back to Top
3. Hibernate Caching principle
The Hibernate cache consists of two main categories:
A.hibernate level cache, also known as the [session cache].
The session can not be uninstalled, the session cache is a transaction-scoped cache (the session object's life cycle usually corresponds to a database transaction or an application transaction).
In the first-level cache, each instance of the persisted class has a unique OID.
B.hibernate level Two cache, also known as [Sessionfactory Cache].
Because the life cycle of the Sessionfactory object corresponds to the entire process of the application.
Hibernate level Two caching is a process-wide or cluster-wide cache, with the potential for concurrency problems and the need for an appropriate concurrency access policy that provides a transaction isolation level for cached data.
The second-level cache is optional and is a configurable plug-in, which is not enabled by default under Sessionfactory.
Hibernate provides the Org.hibernate.cache.CacheProvider interface, which acts as an adapter between the cache plug-in and hibernate.
What data is suitable for storage in the second level cache?
1) rarely modified data
2) data that is not very important, allowing occasional concurrent data to occur
3) data that will not be accessed concurrently
4) Constant data
Not suitable for storing data in a second level cache?
1) Frequently modified data
2) never allow concurrent access to data, such as financial data, is never allowed to appear concurrency
3) data that is shared with other apps.
C.session delay loading implementation To resolve two issues: gracefully close the connection and ensure that the same Session is accessed in the request.
Hibernate session is a high-level package of Java.sql.Connection, a session corresponding to a Connection.
The session is closed correctly after the Http request is completed (the filter realizes the normal shutdown of the session);
Deferred loading must be guaranteed to be the same session (session bound at ThreadLocal).
D.hibernate How does a Lookup object apply a cache?
When Hibernate accesses data objects based on their IDs, the first cache is checked from the Session level.
Not found, if a level two cache is configured, then from the level two cache;
If it is not found, then query the database, the results by ID into the cache to delete, update, add data, while updating the cache.
E. Comparison of first-level cache with level two cache
|
First-level caching |
Second-level cache |
The form of storing data |
Interrelated persisted objects |
object's bulk data |
Scope of the cache |
Transaction scope, each transaction has a separate first-level cache |
Process scope or cluster scope, the cache is shared across all transactions within the same process or cluster |
| Concurrent access policy |
There is no concurrency problem because each transaction has a separate first-level cache, so there is no need to provide a concurrent access policy |
Because multiple transactions access the same data in the level two cache at the same time, the appropriate concurrency access policy must be provided to guarantee a specific transaction isolation level |
| Data Expiration policy |
objects in the first-level cache never expire unless the application displays empty or empty specific objects |
must provide a data expiration policy, such as the maximum number of objects in the memory-based cache, the longest time the object is allowed to be in the cache, and the maximum idle time allowed for the object to be in the cache |
Physical media |
Memory |
Memory and hard disk, the object's bulk data is first stored in the memory-based cache, and when the number of objects in memory reaches the maxelementsinmemory value of the data expiration policy, the rest of the objects are written to the hard disk-based cache |
Cache software Implementation |
Included in the implementation of Hibernate's session |
Provided by a third party, hibernate only provides a cache adapter for integrating specific cache plugins into hibernate |
| How to enable caching |
As long as the session interface to perform save, update, delete, load, query, hibernate will enable the first-level cache, for bulk operations, such as do not want to enable the first-level cache, directly through the JDBCAPI to execute the |
Users can configure a second-level cache on the granularity of a single collection of individual classes or classes, and if an instance of the class is read frequently, but is rarely modified, consider using a level two cache, only a class or collection is configured with a level two cache, and hibernate will add its instance to the two cache at runtime |
How the user manages the cache |
The first-level cache of physical media is memory, due to the limited capacity of memory, the number of loaded objects must be restricted through appropriate retrieval policies and retrieval methods, the session's Evit () method can display the empty cache of specific objects, but not recommended |
Secondary caches of physical media can make memory and hard disk, so the second level cache can hold large volumes of data, the Maxelementsinmemory property of the data expiration policy can control the number of objects in memory, the management of level two cache mainly includes two aspects: Select the persistence class that needs to use the second level cache, Set the appropriate concurrency access policy, select the cache adapter, and set the appropriate data expiration policy. The Sessionfactory Evit () method can also display the empty cache for specific objects, but is not recommended |
Analysis of Hibernate caching mechanism