当前位置: 代码迷 >> Web前端 >> Hibernate二级缓存 - 入门示范(转)
  详细解决方案

Hibernate二级缓存 - 入门示范(转)

热度:331   发布时间:2012-08-27 21:21:56.0
Hibernate二级缓存 ---- 入门示例(转)

转自(http://elf8848.iteye.com/blog/700262)

?

一. 我们使用oscache来演示 , 引入oscache的jar包到工程中.


?

二. 把下载的Hibernate压缩包中的? hibernate-3.2\etc\oscache.properties放入classpath中. 用默认设置就行了.


?

三.修改hibernate.cfg.xml? , 加入如下, 意思启用二级缓存, 指定使用OSCache这个实现.

?

?

Xml代码 复制代码?收藏代码
  1. <!DOCTYPE?hibernate-configuration?PUBLIC ??
  2. ????"-//Hibernate/Hibernate?Configuration?DTD?3.0//EN" ??
  3. ????"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">??
  4. ??
  5. <hibernate-configuration>??
  6. ????<session-factory?>??
  7. ????????<property?name="show_sql">true</property>??
  8. ????????<property?name="hibernate.hbm2ddl.auto">update</property>??
  9. ???????? ??
  10. ????????<property?name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>??
  11. ????????<property?name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>??
  12. ????????????????? ??
  13. ????????<property?name="hibernate.connection.url">jdbc:mysql:///test</property>??
  14. ????????<property?name="hibernate.connection.username">root</property>??
  15. ????????<property?name="hibernate.connection.password">1234</property>??
  16. ???????? ??
  17. ????????<!--?这行不写也行,默认就为true-->??
  18. ????????<property?name="cache.use_second_level_cache">true</property>??
  19. ????????<!--?对Query也开启缓存?,由于命中率太低,建议不开?-->??
  20. ????????<property?name="cache.use_query_cache">true</property>??
  21. ????????<!--?指定使用OSCache这个实现-->??
  22. ????????<property?name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>??
  23. ????????<!--?打开统计信息-->??
  24. ????????<property?name="generate_statistics">true</property>???????? ??
  25. ???????????? ??
  26. ????????<mapping?resource="dao/po/Department.hbm.xml"/>??
  27. ????????<mapping?resource="dao/po/Employee.hbm.xml"/>??
  28. ????????<mapping?resource="dao/po/Person.hbm.xml"/>??
  29. ????????<mapping?resource="dao/po/IdCard.hbm.xml"/>??
  30. ????????<mapping?resource="dao/po/Teacher.hbm.xml"/>??
  31. ????????<mapping?resource="dao/po/Student.hbm.xml"/>??
  32. ????????<mapping?resource="dao/po/User.hbm.xml"/>? ??
  33. ????????<mapping?resource="extends_test/extends_1/Person.hbm.xml"/>??
  34. ????????<mapping?resource="extends_test/extends_2/Person.hbm.xml"/>??
  35. ????????<mapping?resource="extends_test/extends_4/Person.hbm.xml"/>??? ??
  36. ???? ??
  37. ????????<!--?以下类被缓存-->??
  38. ????????<class-cache?class="dao.po.Person"?usage="read-write"/>??
  39. ???????? ??
  40. ????</session-factory>??
  41. </hibernate-configuration>??
<!DOCTYPE hibernate-configuration PUBLIC
	"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
	"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
	<session-factory >
		<property name="show_sql">true</property>
		<property name="hibernate.hbm2ddl.auto">update</property>
		
		<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
		<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
				 
		<property name="hibernate.connection.url">jdbc:mysql:///test</property>
		<property name="hibernate.connection.username">root</property>
		<property name="hibernate.connection.password">1234</property>
		
		<!-- 这行不写也行,默认就为true-->
		<property name="cache.use_second_level_cache">true</property>
		<!-- 对Query也开启缓存 ,由于命中率太低,建议不开 -->
		<property name="cache.use_query_cache">true</property>
		<!-- 指定使用OSCache这个实现-->
		<property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
		<!-- 打开统计信息-->
		<property name="generate_statistics">true</property>		
	    	
		<mapping resource="dao/po/Department.hbm.xml"/>
		<mapping resource="dao/po/Employee.hbm.xml"/>
		<mapping resource="dao/po/Person.hbm.xml"/>
		<mapping resource="dao/po/IdCard.hbm.xml"/>
		<mapping resource="dao/po/Teacher.hbm.xml"/>
		<mapping resource="dao/po/Student.hbm.xml"/>
		<mapping resource="dao/po/User.hbm.xml"/>	
		<mapping resource="extends_test/extends_1/Person.hbm.xml"/>
		<mapping resource="extends_test/extends_2/Person.hbm.xml"/>
		<mapping resource="extends_test/extends_4/Person.hbm.xml"/>	
	
		<!-- 以下类被缓存-->
		<class-cache class="dao.po.Person" usage="read-write"/>
		
	</session-factory>
</hibernate-configuration>

??

?

?

?

四.usage的说明


?

????? "read-only"????????????????????? 只读的,? po不能修改, 修改会抛异常

????? "read-write"??????????????????? 可对PO读写. 不怕并发修改

????? "nonstrict-read-write"???? 非严格的 读写, 并发修改不会问题,? 一般用在能容忍出问题的环境.

????? "transactional"??????????????? 有事务的,缓存 , oscache不是支持,? jboss 有实现.


?

?

?

五. 查询测试, 看看命中几次

?

Java代码 复制代码?收藏代码
  1. package?cache_test; ??
  2. ??
  3. import?org.hibernate.Session; ??
  4. import?org.hibernate.SessionFactory; ??
  5. import?org.hibernate.stat.Statistics; ??
  6. ??
  7. import?dao.HibernateUtil; ??
  8. import?dao.po.Person; ??
  9. ??
  10. /** ?
  11. ?*?测试缓存 ?
  12. ?*? ?
  13. ?*/??
  14. public?class?Test ??
  15. { ??
  16. ????public?static?void?main(final?String[]?args) ??
  17. ????{ ??
  18. ????????query(); ??
  19. ????????final?SessionFactory?sf?=?HibernateUtil.getSessionFactory(); ??
  20. ????????final?Statistics?s?=?sf.getStatistics(); ??
  21. ????????System.out.println(s);?//打印所有信息?监测SessionFactory ??
  22. ????????System.out.println("---------------"); ??
  23. ????????System.out.println("放入:"?+?s.getSecondLevelCachePutCount());?//打印缓存的信息 ??
  24. ????????System.out.println("命中:"?+?s.getSecondLevelCacheHitCount()); ??
  25. ????????System.out.println("丢失:"?+?s.getSecondLevelCacheMissCount()); ??
  26. ????} ??
  27. ??
  28. ????public?static?void?query() ??
  29. ????{ ??
  30. ????????Session?session?=?null; ??
  31. ??
  32. ????????for?(int?i?=?0;?i?<?10;?i++) ??
  33. ????????{ ??
  34. ????????????session?=?HibernateUtil.getSeesion(); ??
  35. ????????????final?Person?p1?=?(Person)?session.get(Person.class,?1); ??
  36. ????????????session.close(); ??
  37. ????????????System.out.println(p1.getName()); ??
  38. ????????} ??
  39. ??
  40. ????} ??
  41. }??
package cache_test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.stat.Statistics;

import dao.HibernateUtil;
import dao.po.Person;

/**
 * 测试缓存
 * 
 */
public class Test
{
	public static void main(final String[] args)
	{
		query();
		final SessionFactory sf = HibernateUtil.getSessionFactory();
		final Statistics s = sf.getStatistics();
		System.out.println(s); //打印所有信息 监测SessionFactory
		System.out.println("---------------");
		System.out.println("放入:" + s.getSecondLevelCachePutCount()); //打印缓存的信息
		System.out.println("命中:" + s.getSecondLevelCacheHitCount());
		System.out.println("丢失:" + s.getSecondLevelCacheMissCount());
	}

	public static void query()
	{
		Session session = null;

		for (int i = 0; i < 10; i++)
		{
			session = HibernateUtil.getSeesion();
			final Person p1 = (Person) session.get(Person.class, 1);
			session.close();
			System.out.println(p1.getName());
		}

	}
}

?

?

六:? 打印出:

放入:1
命中:9
丢失:1

  相关解决方案