2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session
Cache configuration and the use of:
Will echcache.xml (the document code in hibernate package directory etc) are copied to the src directory
Open the secondary cache modified hibernate.cfg.xml file <property name="hibernate.cache.use_second_level_cache"> true </ property>
Product providers designated cache, modify hibernate.cfg.xml file <property name="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </ property>
Specify which type of use 2 Cache:
Used in the mapping file <cache usage=""> Tags
Medium in the hibernate.cfg.xml configuration <class-cache usage="read-only"/>
I recommend priority <read-only>
1. <read-only>
If your application just to read a persistent class instances without having to modify, then it can be read-only cache. This is the simplest, but also the best way to practicality. Even in the cluster, it can also operate perfectly.
2. Read / write
If an application need to update the data, then use the read / write cache more appropriate. If an application request "sequence of affairs" of the isolation level (serializable transaction isolation level), then it must not be using this cache strategy. If in JTA environment, use of cache, you must specify the value of property hibernate.transaction.manager_lookup_ class, which, Hibernate can we know that the application of the JTA transactionManager of specific strategies. In other environments, you must ensure that Session.close (), or Session.disconnect () call before the end of the whole affairs. If you want to in the cluster environment using this strategy, you must ensure that the underlying cache implementation support locking (locking). Hibernate built-in cache strategy does not support the lock function 3. Nonstrict read / write
If an application only occasionally need to update the data (that is, two panels at the same time to update the same records is very common), do not need very strict isolation, then the more appropriate use of non-strict read / write cache strategy. If in JTA environment, use of the strategy, you must assign the value of property hibernate.transaction.manager_lookup_class in other environments, you must ensure that Session.close (), or Session.disconnect () call before has the entire The End
Secondary cache is cached entity object:
Understanding of cross-level cache (session.setCacheMode ())
Mainly CacheMode.NORMAL
CacheMode.PUT
CacheMode.GET
Specific common HibernateReference
Has the following two categories:
Java code:
- public class Clazz (
- private Integer id;
- private String name;
- private Set <Student> students = new HashSet <Student> ();
- )
- public class Student (
- private Integer id;
- private String name;
- private Clazz clazz;
public class Clazz {
private Integer id;
private String name;
private Set<Student> students=new HashSet<Student>();
}
public class Student {
private Integer id;
private String name;
private Clazz clazz;
}
Mapping file for (we set the cache type of students):
Xml Code:
- <class name="com.june.hibernate.Clazz" table="t_clazz">
- <id name="id">
- <generator class="native"> </ generator>
- </ id>
- <property name="name"> </ property>
- <set name="students" inverse="true">
- <key column="clazz" />
- <one-to-many class="com.june.hibernate.Student" />
- </ set>
- </ class>
- <class name="com.june.hibernate.Student" table="t_student">
- <! --
- <cache usage="read-only"/> ->
- <id name="id">
- <generator class="native"> </ generator>
- </ id>
- <property name="name"> </ property>
- <many-to-one name="clazz" cascade="save-update"> </ many-to-one>
- </ class>
<class name="com.june.hibernate.Clazz" table="t_clazz"> <id name="id"> <generator></generator> </id> <property name="name"></property> <set name="students" inverse="true"> <key column="clazz"/> <one-to-many/> </set> </class> <class name="com.june.hibernate.Student" table="t_student"> <!-- <cache usage="read-only"/>--> <id name="id"> <generator></generator> </id> <property name="name"></property> <many-to-one name="clazz" cascade="save-update"></many-to-one> </class>
Hibernate configuration file:
Xml Code:
- <hibernate-configuration>
- <session-factory>
- <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </ property>
- <property name="hibernate.connection.url"> jdbc: mysql: / / localhost: 3306/hibernate_cache_level_2 </ property>
- <property name="hibernate.connection.username"> root </ property>
- <property name="hibernate.connection.password"> 123456 </ property>
- <property name="hibernate.dialect"> org.hibernate.dialect.MySQLDialect </ property>
- <property name="hibernate.show_sql"> true </ property>
- <! - Open the hibernate cache is turned on by default ->
- <property name="hibernate.cache.use_second_level_cache"> true </ property>
- <! - Specify the cache provider Products ->
- <property name="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </ property>
- <mapping resource="com/june/hibernate/Clazz.hbm.xml" />
- <mapping resource="com/june/hibernate/Student.hbm.xml" />
- <! - I recommend in this setting rather than the secondary cache in specific types of configuration files to set up because in this setting would have a very clear understanding of those who set up the secondary cache are those that are not established ->
- <class-cache class="com.june.hibernate.Student" usage="read-only" />
- </ session-factory>
- </ hibernate-configuration>
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/hibernate_cache_level_2</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<!-- 开启二级缓存 hibernate默认就是开启的 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 指定缓存产品提供商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<mapping resource="com/june/hibernate/Clazz.hbm.xml"/>
<mapping resource="com/june/hibernate/Student.hbm.xml"/>
<!-- 我推荐在此设置二级缓存 而不是在具体的类配置文件中设置 因为在此设置会对非常清楚的了解 那些是设了二级缓存的 那些是没有设的-->
<class-cache usage="read-only"/>
</session-factory>
</hibernate-configuration>
Test Case:
Java code
package com.june.hibernate;
import junit.framework.TestCase;
import org.hibernate.CacheMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class CacheLevel2Test extends TestCase (
/ **
* Open two session separately call load
* /
public void testLoad () (
Session session = null;
Transaction tx = null;
try (
/ / The first session
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
/ / Read data from cache rather than from the secondary data cache Writing session.setCacheMode (CacheMode.GET);
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
/ / Will not issue a sql statement System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
SessionFactory factory = HibernateUtil.getSessionFactory ();
try (
/ / The first three session
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
/ / Set up session because CacheMode bit GET 2 cache so there is no data System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
public void testCache2 () (
Session session = null;
Transaction tx = null;
try (
/ / The first session
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
try (
/ / The first two session
session = HibernateUtil.getSession ();
/ / Read from secondary relieve Writing data without relief from the secondary read and write data session.setCacheMode (CacheMode.PUT);
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
SessionFactory factory = HibernateUtil.getSessionFactory ();
try (
/ / The first three session
session = HibernateUtil.getSession ();
tx = session.beginTransaction ();
Student student1 = (Student) session.load (Student.class, 1);
/ / Set up session because CacheMode bit GET 2 cache so there is no data System.out.println ( "student1.name =" + student1.getName ());
tx.commit ();
) catch (Exception e) (
e.printStackTrace ();
tx.rollback ();
) finally (
HibernateUtil.colseSession (session);
)
)
)








Responses to “Hibernate secondary cache”