hibernate3.2二级缓存的配置及测试(ehcache)
来源:互联网 发布:mysql重做 编辑:程序博客网 时间:2024/05/29 18:04
1.配置ehcache.xml文件,放到classpath下:
<?xml version="1.0" encoding="GBK"?>
<ehcache>
<diskStore path="D://TempObject"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="1000"
overflowToDisk="true"
/>
<cache name="com.sitechasia.occ.core.base.ExampleForTest" maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="1000"
overflowToDisk="true"
/>
</ehcache>
建议自定义cache时,cache名字和类路径名相同。
(1)不要使用默认缓存策略defaultCache(多个class共享)
(2)不要给cache name另外起名
否则继承AbstractTransactionalDataSourceSpringContextTests做测试时,抛出
org.hibernate.cache.CacheException: java.lang.IllegalStateException: The com.sitechasia.occ.core.base.ExampleForTest Cache is not alive.(我注释了红色的cache,使用defaultCache导致)
2.在ExampleForTest.hbm.xml中添加:(如果有集合,也需要添加)
<hibernate-mapping>
<class name="com.sitechasia.occ.core.base.ExampleForTest"
table="TESTTABLE" lazy="false">
<cache usage="read-write"/>
<id name="id" type="java.lang.String">
<column name="id" length="32" />
<generator class="uuid"></generator>
</id>
<property name="field1" type="java.lang.String" />
<property name="field2" type="java.lang.String" />
</class>
</hibernate-mapping>
如果使用Annocation,则类前添加
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Proxy(lazy = false)
3.在spring.xml中添加:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 使用hibernate自带cache,适合测试用
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</prop>
-->
<!-- 使用ehcache,适合项目用 -->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<!-- 打开hibernate统计功能,我们测试中使用hibernate的统计类-->
<prop key="hibernate.generate_statistics">true</prop>
<!-- 最优化二级缓存-->
<prop key="hibernate.cache.use_structured_entries">
true
</prop>
<!-- 完全禁用二级缓存开关,对那些在类的映射定义中指定cache的类,默认开启二级缓存-->
<prop key="cache.use_second_level_cache">true</prop>
<!-- prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.default_schema">
${dbunit.schema}
</prop>
-->
</props>
</property>
4.配置完了,我们可以测试了:
测试类继承spring的测试组件AbstractTransactionalDataSourceSpringContextTests类;
先初始化数据:
private ExampleForTest example1, example2;
public void onSetUpBeforeTransaction() {
example1 = new ExampleForTest();
example1.setField1("param1");
example1.setField2("param2");
example2 = new ExampleForTest();
example2.setField1("attribute1");
example2.setField2("attribute2");
}
再写测试方法:
exampleDao中有2个方法:
(1)list方式获取参数:(对二级缓存只写不读)
exampleDao.findByHQL(hql);
(2)iterate方式获取参数:(对二级缓存可写可读)
exampleDao.cacheByHQL(hql);
测试(2)方法:
public void testCacheByHQL() {
/**先打开注释,往数据库增加2条数据,然后关闭
exampleDao.save(example1);
exampleDao.save(example2);
setComplete();
*/
String hql = "from ExampleForTest";
//List list = exampleDao.findByHQL(hql); //开关控制
List<ExampleForTest> iterate = exampleDao.cacheByHQL(hql);
Statistics statistics = sessionFactory.getStatistics();
// 统计entity信息
for (int i = 0; i < statistics.getEntityNames().length; i++) {
String entityName = statistics.getEntityNames()[i];
System.out.println("entityName:" + entityName);
EntityStatistics entityStatistics = statistics
.getEntityStatistics(entityName);
System.out.println("entityStatistics:" + entityStatistics);
}
assertEquals(2, iterate.size());
}
1》注释开关控制代码行,iterate获取数据,发现有N+1问题,说明是从数据库读取的数据,日志如下:
Hibernate: select examplefor0_.id as col_0_0_ from TESTTABLE examplefor0_
Hibernate: select examplefor0_.id as id0_0_, examplefor0_.field1 as field2_0_0_, examplefor0_.field2 as field3_0_0_ from TESTTABLE examplefor0_ where examplefor0_.id=?
Hibernate: select examplefor0_.id as id0_0_, examplefor0_.field1 as field2_0_0_, examplefor0_.field2 as field3_0_0_ from TESTTABLE examplefor0_ where examplefor0_.id=?
entityName:com.sitechasia.occ.core.base.ExampleForTest
entityStatistics:EntityStatistics[loadCount=2,updateCount=0,insertCount=0,deleteCount=0,fetchCount=2,optimisticLockFailureCount=0]
2》打开开关控制代码行注释,先list方式读取数据,会写入二级缓存,再用iterate获取数据时,发现没有了N+1问题,说明是从二级缓存中读取的数据,日志如下:
Hibernate: select examplefor0_.id as id0_, examplefor0_.field1 as field2_0_, examplefor0_.field2 as field3_0_ from TESTTABLE examplefor0_
Hibernate: select examplefor0_.id as col_0_0_ from TESTTABLE examplefor0_
entityName:com.sitechasia.occ.core.base.ExampleForTest
entityStatistics:EntityStatistics[loadCount=2,updateCount=0,insertCount=0,deleteCount=0,fetchCount=0,optimisticLockFailureCount=0]
5.需要注意的是,实体映射文件中lazy=false必须设置,即立即加载;否则后台日志不打印那N条通过id获取实体的sql,无法判断是否使用二级缓存。
<ehcache>
<diskStore path="D://TempObject"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="1000"
overflowToDisk="true"
/>
<cache name="com.sitechasia.occ.core.base.ExampleForTest" maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="100"
timeToLiveSeconds="1000"
overflowToDisk="true"
/>
</ehcache>
建议自定义cache时,cache名字和类路径名相同。
(1)不要使用默认缓存策略defaultCache(多个class共享)
(2)不要给cache name另外起名
否则继承AbstractTransactionalDataSourceSpringContextTests做测试时,抛出
org.hibernate.cache.CacheException: java.lang.IllegalStateException: The com.sitechasia.occ.core.base.ExampleForTest Cache is not alive.(我注释了红色的cache,使用defaultCache导致)
2.在ExampleForTest.hbm.xml中添加:(如果有集合,也需要添加)
<hibernate-mapping>
<class name="com.sitechasia.occ.core.base.ExampleForTest"
table="TESTTABLE" lazy="false">
<cache usage="read-write"/>
<id name="id" type="java.lang.String">
<column name="id" length="32" />
<generator class="uuid"></generator>
</id>
<property name="field1" type="java.lang.String" />
<property name="field2" type="java.lang.String" />
</class>
</hibernate-mapping>
如果使用Annocation,则类前添加
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Proxy(lazy = false)
3.在spring.xml中添加:
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- 使用hibernate自带cache,适合测试用
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</prop>
-->
<!-- 使用ehcache,适合项目用 -->
<prop key="hibernate.cache.provider_class">
org.hibernate.cache.EhCacheProvider
</prop>
<!-- 打开hibernate统计功能,我们测试中使用hibernate的统计类-->
<prop key="hibernate.generate_statistics">true</prop>
<!-- 最优化二级缓存-->
<prop key="hibernate.cache.use_structured_entries">
true
</prop>
<!-- 完全禁用二级缓存开关,对那些在类的映射定义中指定cache的类,默认开启二级缓存-->
<prop key="cache.use_second_level_cache">true</prop>
<!-- prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.default_schema">
${dbunit.schema}
</prop>
-->
</props>
</property>
4.配置完了,我们可以测试了:
测试类继承spring的测试组件AbstractTransactionalDataSourceSpringContextTests类;
先初始化数据:
private ExampleForTest example1, example2;
public void onSetUpBeforeTransaction() {
example1 = new ExampleForTest();
example1.setField1("param1");
example1.setField2("param2");
example2 = new ExampleForTest();
example2.setField1("attribute1");
example2.setField2("attribute2");
}
再写测试方法:
exampleDao中有2个方法:
(1)list方式获取参数:(对二级缓存只写不读)
exampleDao.findByHQL(hql);
(2)iterate方式获取参数:(对二级缓存可写可读)
exampleDao.cacheByHQL(hql);
测试(2)方法:
public void testCacheByHQL() {
/**先打开注释,往数据库增加2条数据,然后关闭
exampleDao.save(example1);
exampleDao.save(example2);
setComplete();
*/
String hql = "from ExampleForTest";
//List list = exampleDao.findByHQL(hql); //开关控制
List<ExampleForTest> iterate = exampleDao.cacheByHQL(hql);
Statistics statistics = sessionFactory.getStatistics();
// 统计entity信息
for (int i = 0; i < statistics.getEntityNames().length; i++) {
String entityName = statistics.getEntityNames()[i];
System.out.println("entityName:" + entityName);
EntityStatistics entityStatistics = statistics
.getEntityStatistics(entityName);
System.out.println("entityStatistics:" + entityStatistics);
}
assertEquals(2, iterate.size());
}
1》注释开关控制代码行,iterate获取数据,发现有N+1问题,说明是从数据库读取的数据,日志如下:
Hibernate: select examplefor0_.id as col_0_0_ from TESTTABLE examplefor0_
Hibernate: select examplefor0_.id as id0_0_, examplefor0_.field1 as field2_0_0_, examplefor0_.field2 as field3_0_0_ from TESTTABLE examplefor0_ where examplefor0_.id=?
Hibernate: select examplefor0_.id as id0_0_, examplefor0_.field1 as field2_0_0_, examplefor0_.field2 as field3_0_0_ from TESTTABLE examplefor0_ where examplefor0_.id=?
entityName:com.sitechasia.occ.core.base.ExampleForTest
entityStatistics:EntityStatistics[loadCount=2,updateCount=0,insertCount=0,deleteCount=0,fetchCount=2,optimisticLockFailureCount=0]
2》打开开关控制代码行注释,先list方式读取数据,会写入二级缓存,再用iterate获取数据时,发现没有了N+1问题,说明是从二级缓存中读取的数据,日志如下:
Hibernate: select examplefor0_.id as id0_, examplefor0_.field1 as field2_0_, examplefor0_.field2 as field3_0_ from TESTTABLE examplefor0_
Hibernate: select examplefor0_.id as col_0_0_ from TESTTABLE examplefor0_
entityName:com.sitechasia.occ.core.base.ExampleForTest
entityStatistics:EntityStatistics[loadCount=2,updateCount=0,insertCount=0,deleteCount=0,fetchCount=0,optimisticLockFailureCount=0]
5.需要注意的是,实体映射文件中lazy=false必须设置,即立即加载;否则后台日志不打印那N条通过id获取实体的sql,无法判断是否使用二级缓存。
- hibernate3.2二级缓存的配置及测试(ehcache)
- Hibernate3.6二级缓存配置 - ehcache
- hibernate二级缓存:hibernate3 2级缓存Cache的配置及测试
- hibernate3-二级缓存配制 EHCache实战
- Hibernate ehcache配置二级缓存及说明
- Hibernate ehcache 配置二级缓存及说明
- Hibernate ehcache配置二级缓存及说明
- ehcache二级缓存配置(SSH缓存的配置) (转)
- 二级缓存的应用(ehcache)
- Hibernate3 二级缓存的使用配置细节
- Hibernate3 二级缓存的使用配置细节
- Hibernate3 二级缓存的使用配置细节
- Hibernate二级缓存插件EHCache的配置
- Hibernate二级缓存以及ehcache的搭建配置
- Hibernate二级缓存以及ehcache的搭建配置
- Hibernate+ehcache二级缓存配置
- Hibernate ehcache配置二级缓存
- SSH ehcache二级缓存配置
- 期货与期权
- 基于TCP/IP的局域网多用户通信
- ASP.NET下MVC设计模式的实现
- windows 2003 下Server.CreateObject 不能访问的解决办法(转)
- RAS拨号软件源代码 V2.0
- hibernate3.2二级缓存的配置及测试(ehcache)
- Java混乱器Zelix KlassMaster逆向工程(五) 针对 ZKM处理 for/while/switch的介绍
- php4的mysql函数库
- CURL类库返回说明
- Web编解码方式集合
- ArcGIS开发常用术语
- 一根网线连接两台电脑
- 总结必须学习的10项.NET技术
- 如何在字符串中输入引号?