Important knowledge points for hibernate level two cache
Second-level caching is also known as process-level caching or sessionfactory-level caching, and level two caching can be shared by all sessions
The second-level cache lifecycle is consistent with the sessionfactory lifecycle, Sessionfactory can manage level two caching
Hibernate provides a two-level cache implementation based on Hashtable, but it is recommended for use only in the development phase, where third-party-provided cache products should be used in a production environment
Configuration and use of secondary caching: (This sample is based on third party product Echcache)
* Copy {Hibernate_home}/etc/echcache.xml file to src
* Open Level Two cache, modify Hibernate.cfg.xml file
<property name= "Hibernate.cache.use_second_level_cache" >true</property>
* Specify cache product provider, modify Hibernate.cfg.xml file
<property name= "Hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</property>
* Specifies that those entity classes use level two caching (two methods)
* Use <cache> tags in the mapping file
* In the Hibernate.cfg.xml file, the use of <class-cache> tags
The second-level cache is a cache of entity objects.
Understanding primary and level two cache interactions
1. Configure Echcache.xml files
Place the Echcache.xml file in the SRC directory with the hibernate.cfg.xml level directory
2. Modify the hibernate.cfg.xml file
<! DOCTYPE hibernate-configuration Public
"-//hibernate/hibernate Configuration DTD 3.0//en"
"Http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
<session-factory>
<property name= "Hibernate.connection.url" >jdbc:mysql://localhost/hibernate_cache</property>
<property name= "Hibernate.connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name= "Hibernate.connection.username" >root</property>
<property name= "Hibernate.connection.password" >bjsxt</property>
<property name= "Hibernate.dialect" >org.hibernate.dialect.MySQLDialect</property>
<property name= "Hibernate.show_sql" >true</property>
<!--turn on level two cache, default is open-->
<property name= "Hibernate.cache.use_second_level_cache" >true</property>
<!--Specify a caching product provider, this example uses Echcache-->
<property name= "Hibernate.cache.provider_class" >org.hibernate.cache.EhCacheProvider</property>
<mapping resource= "Com/bjsxt/hibernate/classes.hbm.xml"/>
<mapping resource= "Com/bjsxt/hibernate/student.hbm.xml"/>
<!--specifies entity classes that use level two caching, or you can configure--> in the class mapping file
<class-cache class= "com.bjsxt.hibernate.Student" usage= "Read-only"/>
</session-factory>
3. Writing test Cases
Package com.bjsxt.hibernate;
Import java.io.Serializable;
Import Org.hibernate.CacheMode;
Import org.hibernate.Session;
Import Org.hibernate.SessionFactory;
Import Junit.framework.TestCase;
public class Cachelevel2test extends TestCase {
/**
* Open two sessions, call load separately
*/
public void TestCache1 () {
Session session = NULL;
try {
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Student Student = (Student) session.load (Student.class, 1);
System.out.println ("student.name=" + student.getname ());
Session.gettransaction (). commit ();
}catch (Exception e) {
E.printstacktrace ();
Session.gettransaction (). rollback ();
}finally {
Hibernateutils.closesession (session);
}
try {
Session = Hibernateutils.getsession ();
Session.begintransaction ();
SQL is not emitted because the level two cache is turned on and session is shared with level two cache
Student Student = (Student) session.load (Student.class, 1);
System.out.println ("student.name=" + student.getname ());
Session.gettransaction (). commit ();
}catch (Exception e) {
E.printstacktrace ();
Session.gettransaction (). rollback ();
}finally {
Hibernateutils.closesession (session);
}
}
/**
* Open two sessions, call get
*/
public void TestCache2 () {
Session session = NULL;
try {
Session = Hibernateutils.getsession ();
Session.begintransaction ();
Student Student = (Student) session.get (Student.class, 1);
System.out.println ("student.name=" + student.getname ());