In contrast to the first-level cache of the session, Sessionfactory also provides the appropriate caching mechanism (level two cache). The sessionfactory cache can be divided into built-in caches and external caches, depending on functionality and purpose.
The Sessionfactory's built-in cache contains mapping metadata and predefined SQL statements, which are copies of the data in the mapping file, and the predefined SQL statements are derived from the mapping metadata during the hibernate initialization phase.
Sessionfactory's built-in cache is read-only, and the application cannot modify the mapping metadata and predefined SQL statements in the cache, so sessionfactory does not need to synchronize the built-in cache with the mapped file.
Sessionfactory's external cache is a configurable plug-in. By default, Sessionfactory does not enable this plugin. The externally cached data is a copy of the database data,
The external cache media can be either memory or hard disk. Sessionfactory's external cache is also known as Hibernate's Level two cache.
Hibernate's level two cache is implemented in the same way as a first-level cache, and is achieved by caching the object with a map of the ID key.
Since Hibernate's level two cache is scoped to the sessionfactory, it is more extensive than a first-level cache and can be shared by all session objects.
Describe Hibernate's level two cache
Answer according to the following ideas:
(1) First clarify what is the cache,
(2) Besides, the session of Hibernate is a first-level cache, that is, a cache,
Why do we have a level two cache,
(3) Finally, how to configure Hibernate's level two cache.
(1) The cache is to save objects previously queried and used from the database in memory (a data structure), the data structure is usually or
Similar to HashMap, when you want to use an object in the future, first query the cache for this object, if any, use the object in the cache, if
There is no way to query the database and save the queried object in the cache for the next use. Here is the pseudo-code for the cache:
The second level cache of Hibernate is drawn, and the implementation principle of the cache is analyzed with the following pseudo code
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) Hibernate session is a kind of cache, we usually call it hibernate cache, when you want to use the session from the database to query an object, the session is to see from their own internal existence of the object, the existence is directly returned, Does not exist to access the database and save the query's results inside itself. Since the session represents a conversation process, the sessions are connected to a database connection,
So it's best not to keep it open for long periods of time, usually only in one transaction, and close at the end of the transaction. And the session is thread insecure,
The problem is prone to being shared by multiple threads. Usually only the global cache is the real cache application, there is a large cache value,
Therefore, the cache function of Hibernate session is not obvious and the application value is not very significant.
Hibernate's Level Two cache is a global cache configured for Hibernate, allowing multiple threads and multiple transactions to share this cache.
We want a person to use it, others can use it, and the session doesn't have that effect.
(3) Level two cache is a software part that is independent of hibernate, a product belonging to a third party, a cache product is provided by multiple vendors and organizations,
For example, Ehcache and Oscache and so on. Using a level two cache in Hibernate, first configure the use in the Hibernate.cfg.xml configuration file
Which manufacturer's cache product, then need to configure the cache product's own configuration file, and finally configure which entity objects in hibernate to be included in the two
-Level cache management. Understanding the two-level caching principle and with this idea, it is easy to configure Hibernate's level two cache.
Extended Knowledge:
A sessionfactory can be associated with a level two cache, or a level two cache can only be responsible for caching data in a database when using hibernate
Level two cache, be careful not to have other apps or sessionfactory to change the data in the current database so that the cached data will be associated with the database
Inconsistent with the actual data in the.
Hibernate's Level two cache