- 浏览: 1212286 次
- 性别:
- 来自: 广州
博客专栏
-
Apache CXF使用s...
浏览量:109829
文章分类
- 全部博客 (189)
- Spring (13)
- Hibernate (10)
- liferay portal (3)
- Java (18)
- 数据库 (32)
- JS (12)
- Eclipse (16)
- Log4j (3)
- 我的收藏夹 (8)
- FF and IE (2)
- Groovy Grails (0)
- Web Service (16)
- Quartz (6)
- Tomcat (8)
- Linux (4)
- xml (4)
- Silverlight (1)
- Flex (10)
- JBoss (4)
- EJB (2)
- WAS(webSphere) (5)
- DOS命令 (2)
- JSON (2)
- Maven (1)
- OThers (1)
- SVN (1)
- iBatis (4)
- OS (1)
- 问题解决 (8)
- 待看文章 (2)
- 多线程 (2)
- 代码收藏(即拿即用工具类) (5)
- Socket (2)
- Android (4)
- 其他 (1)
- python (1)
- Genymotion (1)
最新评论
-
a807966224:
还是 多谢楼主 总结的挺好的 !!!
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
a807966224:
然后 通过 SOAPMessage.getHeader(qna ...
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
a807966224:
我也是接触这东西不久,QName qname = new QN ...
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
a807966224:
楼主 不知道你有没有出现 从headers 里取出来长 ...
CXF 入门:创建一个基于SOAPHeader的安全验证(CXF拦截器使用) -
xdc0209:
兄弟呀,报错啦 2011-12-15 13:27:15 n ...
Hibernate+EhCache配置二级缓存
FROM: http://sjsky.iteye.com/blog/1312132
本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法,主要分以下两个方面介绍:
(有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 )
- Cache的多种配置方法
- Hibernate+EhCache集成demo
[一]、Cache的多种配置方法
Javabean cache的配置有三种,下面将一一介绍,具体如下::
(1).bean中注解配置的方式: @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
(2).hibernate.cfg.xml中标签配置方式: <class-cache class="" usage="" />
(3).映射文件*.hb.xml中标签配置方式: <cache usage=" />
1. classpath:ehcahce.xml配置文件如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:noNamespaceSchemaLocation="ehcache.xsd"
- updateCheck="true" monitoring="autodetect"
- dynamicConfig="true">
- <diskStore path="java.io.tmpdir"/>
- <transactionManagerLookup class="net.sf.ehcache.transaction.manager.DefaultTransactionManagerLookup"
- properties="jndiName=java:/TransactionManager" propertySeparator=";"/>
- <cacheManagerEventListenerFactory class="" properties=""/>
- <defaultCache
- maxElementsInMemory="100"
- eternal="false"
- timeToIdleSeconds="120"
- timeToLiveSeconds="120"
- overflowToDisk="true"
- diskSpoolBufferSizeMB="30"
- maxElementsOnDisk="100"
- diskPersistent="false"
- diskExpiryThreadIntervalSeconds="120"
- memoryStoreEvictionPolicy="LRU"
- statistics="false"
- />
- <cache name="org.hibernate.cache.StandardQueryCache"
- maxElementsInMemory="5"
- eternal="false"
- timeToLiveSeconds="120"
- overflowToDisk="true" />
- <cache name="org.hibernate.cache.UpdateTimestampsCache"
- maxElementsInMemory="5000"
- eternal="true"
- overflowToDisk="true" />
- <cache name="sampleCache1"
- maxElementsInMemory="10000"
- maxElementsOnDisk="1000"
- eternal="false"
- overflowToDisk="true"
- diskSpoolBufferSizeMB="20"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- memoryStoreEvictionPolicy="LFU"
- transactionalMode="off"
- />
- <cache name="sampleCache2"
- maxElementsInMemory="1000"
- eternal="true"
- overflowToDisk="false"
- memoryStoreEvictionPolicy="FIFO"
- />
- </ehcache>
2.hibernate配置文件:hibernate.cfg.xml
- <!DOCTYPE hibernate-configuration
- PUBLIC "-//Hibernate/Hibernate Configuration DTD//EN"
- "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
- <hibernate-configuration>
- <session-factory>
- <property name="connection.driver_class">
- oracle.jdbc.driver.OracleDriver
- </property>
- <property name="connection.url">
- jdbc:oracle:thin:@localhost:1521:ora11g
- </property>
- <property name="connection.username">mytest</property>
- <property name="connection.password">111111</property>
- <property name="dialect">
- org.hibernate.dialect.Oracle9Dialect
- </property>
- <property name="connection.useUnicode">true</property>
- <property name="connection.characterEncoding">UTF-8</property>
- <property name="connection.SetBigStringTryClob">true</property>
- <property name="connection.pool_size">10</property>
- <property name="hibernate.jdbc.batch_size">10</property>
- <property name="show_sql">true</property>
- <property name="format_sql">false</property>
- <property name="current_session_context_class">thread</property>
- <property name="hbm2ddl.auto">update</property>
- <!-- Hibernate 3.3 and higher -->
- <!--
- <property name="hibernate.cache.region.factory_class">
- net.sf.ehcache.hibernate.EhCacheRegionFactory
- </property>
- <property name="hibernate.cache.region.factory_class">
- net.sf.ehcache.hibernate.SingletonEhCacheRegionFactory
- </property>
- -->
- <!-- hibernate3.0-3.2 cache config-->
- <!--
- <property name="hibernate.cache.region.factory_class">
- net.sf.ehcache.hibernate.EhCacheProvider
- </property>
- -->
- <property name="hibernate.cache.provider_class">
- net.sf.ehcache.hibernate.SingletonEhCacheProvider
- </property>
- <!-- Enable Second-Level Cache and Query Cache Settings -->
- <property name="hibernate.cache.use_second_level_cache">
- true
- </property>
- <property name="hibernate.cache.use_query_cache">
- true
- </property>
- <!-- 注解配置 -->
- <mapping class="michael.cache.ehcache.hibernate.EhUserInfo" />
- <mapping class="michael.cache.ehcache.hibernate.EhBlogTopic" />
- <mapping class="michael.cache.ehcache.hibernate.EhBlogTopic2" />
- <!-- 映射文件 -->
- <mapping
- resource="michael/cache/ehcache/hibernate/tb_EhBlogTopic3.hb.xml" />
- <!-- class-cache config -->
- <class-cache class="michael.cache.ehcache.hibernate.EhBlogTopic"
- usage="read-write" />
- </session-factory>
- </hibernate-configuration>
3.相关javabean代码片段如下:
(1).hibernate.cfg.xml中<calss-cache>标签配置的:EhBlogTopic.java:
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Entity
- @Table(name = "MY_TB_EH_BLOG_TOPIC")
- public class EhBlogTopic implements Serializable {
- /**
- * serialVersionUID
- */
- private static final long serialVersionUID = -570936907944909799L;
- private Integer id;
- private String userId;
- private String topic;
- private String site;
- //其他省略
- }
(2). bean中注解的方式配置cache的:EhBlogTopic2.java
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Entity
- @Table(name = "MY_TB_EH_BLOG_TOPIC2")
- @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
- public class EhBlogTopic2 implements Serializable {
- //属性和EhBlogTopic一样
- //其他省略
- }
(3). 映射文件*.hb.xml中添加cache标签的: EhBlogTopic3.java
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class EhBlogTopic3 implements Serializable {
- //属性和EhBlogTopic一样
- //其他省略
- }
tb_EhBlogTopic3.hb.xml
- <?xml version="1.0"?>
- <!DOCTYPE hibernate-mapping PUBLIC
- "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
- "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
- <hibernate-mapping package="michael.cache.ehcache.hibernate">
- <class name="EhBlogTopic3" table="MY_TB_EH_BLOG_TOPIC3">
- <cache usage="read-write" />
- <id name="id" type="int" unsaved-value="null">
- <generator class="increment" />
- </id>
- <property name="userId" column="USER_ID" type="string"
- not-null="false" length="20" />
- <property name="topic" column="TOPIC" type="string"
- not-null="false" length="100" />
- <property name="site" column="SITE" type="string"
- not-null="false" length="100" />
- </class>
- </hibernate-mapping>
(4). 没有配置cache的bean:EhUserInfo.java
- /**
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- @Entity
- @Table(name = "MY_TB_EH_USER_INFO")
- public class EhUserInfo implements Serializable {
- /**
- * serialVersionUID
- */
- private static final long serialVersionUID = 930384253681679239L;
- private Integer id;
- private String userId;
- private String userName;
- private String otherInfo;
- /**
- * @return the id
- */
- @Id
- @GeneratedValue
- @Column(name = "ID")
- public Integer getId() {
- return id;
- }
- //其他省略。。。
- }
4.测试运行代码如下:
- package michael.cache.ehcache.hibernate;
- import java.util.List;
- import michael.hibernate.bigstring.oracle.BigStrBlob;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.AnnotationConfiguration;
- import org.hibernate.cfg.Configuration;
- /**
- *
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class TestEhcacheHibernate {
- /**
- * @param args
- */
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- testMulitConfigMethod();
- }
- /**
- * 测试多种配置缓存的方法
- */
- public static void testMulitConfigMethod() {
- SessionFactory sessionFactory = null;
- try {
- System.out.println("ehcache - hibernate Test ...");
- Configuration config = new AnnotationConfiguration()
- .configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
- System.out.println("hibernate config successful :" + config);
- sessionFactory = config.buildSessionFactory();
- Transaction ta = null;
- try {
- Session session = sessionFactory.getCurrentSession();
- ta = session.beginTransaction();
- } catch (Exception e) {
- e.printStackTrace();
- ta.rollback();
- }
- String[] cacheNames = CacheManager.getInstance().getCacheNames();
- System.out.println("缓存的key cacheNames length := "
- + cacheNames.length + " 具体详细列表如下:");
- for (String name : cacheNames) {
- System.out.println("name := " + name);
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("ehcache - hibernate Test end.");
- }
- }
运行结果如下:
hibernate config successful :org.hibernate.cfg.AnnotationConfiguration@193c0cf
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
2011-12-15 11:32:36 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
2011-12-15 11:32:37 net.sf.ehcache.util.UpdateChecker doCheck
信息: New update(s) found: 2.4.6 [http://www.terracotta.org/confluence/display/release/Release+Notes+Ehcache+Core+2.4]. Please check http://ehcache.org for the latest version.
缓存的key cacheNames length := 7 具体详细列表如下:
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
ehcache - hibernate Test end.
从运行结果可见:三种方式的缓存配置都已经成功。
[二]、Hibernate+EhCache集成demo
1.分别初始化EhUserInfo(没有配置cache的)和EhBlogTopic(配置过cache的)数据如下:
EhUserInfo:
EhBlogTopic:
2.演示代码:
- package michael.cache.ehcache.hibernate;
- import java.util.List;
- import michael.hibernate.bigstring.oracle.BigStrBlob;
- import net.sf.ehcache.Cache;
- import net.sf.ehcache.CacheManager;
- import org.hibernate.Query;
- import org.hibernate.Session;
- import org.hibernate.SessionFactory;
- import org.hibernate.Transaction;
- import org.hibernate.cfg.AnnotationConfiguration;
- import org.hibernate.cfg.Configuration;
- /**
- *
- * @blog http://sjsky.iteye.com
- * @author Michael
- */
- public class TestEhcacheHibernate {
- /**
- * @param args
- */
- @SuppressWarnings("unchecked")
- public static void main(String[] args) {
- SessionFactory sessionFactory = null;
- try {
- System.out.println("ehcache - hibernate Test ...");
- Configuration config = new AnnotationConfiguration()
- .configure("michael/cache/ehcache/hibernate/hibernate.cfg.xml");
- sessionFactory = config.buildSessionFactory();
- System.out.println("buildSessionFactory===============");
- System.out.println("====================================");
- System.out.println("session open ....");
- System.out.println("同一个session(一级缓存默认的)中,没有配置cache的Bean");
- Transaction ta = null;
- try {
- Session session = sessionFactory.getCurrentSession();
- String hsql = "select t from EhUserInfo t ";
- ta = session.beginTransaction();
- Query query = session.createQuery(hsql);
- System.out.println("查询全部query.list().size:"
- + query.list().size());
- System.out.println("再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql");
- EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
- System.out.println("根据ID=1找到的记录:" + vo1);
- ta.commit();
- } catch (Exception e) {
- e.printStackTrace();
- ta.rollback();
- }
- System.out.println("session closed.");
- System.out.println("session open ....");
- try {
- Session session = sessionFactory.getCurrentSession();
- ta = session.beginTransaction();
- System.out.println("第一次根据ID=1查询某记录时,会打印hsql");
- EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
- System.out.println("根据ID=1找到的记录:" + vo1);
- ta.commit();
- } catch (Exception e) {
- e.printStackTrace();
- ta.rollback();
- }
- System.out.println("session closed.");
- System.out.println("====================================");
- System.out.println("当前同一个sessionFactory,没有配置cache的Bean,");
- System.out.println("session open ....");
- try {
- Session session = sessionFactory.getCurrentSession();
- ta = session.beginTransaction();
- System.out.println("第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:");
- EhUserInfo vo1 = (EhUserInfo) session.get(EhUserInfo.class, 1);
- System.out.println("根据ID=1找到的记录:" + vo1);
- ta.commit();
- } catch (Exception e) {
- e.printStackTrace();
- ta.rollback();
- }
- System.out.println("session closed.");
- System.out.println("====================================");
- String[] cacheNames = CacheManager.getInstance().getCacheNames();
- System.out.println("缓存的key cacheNames length := "
- + cacheNames.length);
- for (String name : cacheNames) {
- System.out.println("name := " + name);
- }
- System.out.println("====================================");
- System.out.println("同一个session(一级缓存默认的)中,配置cache的Bean");
- System.out.println("session open ....");
- try {
- Session session = sessionFactory.getCurrentSession();
- String hsql = "select t from EhBlogTopic t ";
- ta = session.beginTransaction();
- Query query = session.createQuery(hsql);
- query.setCacheable(true);
- System.out.println("查询全部query.list().size:"
- + query.list().size());
- Cache myCache1 = CacheManager.getInstance().getCache(
- "michael.cache.ehcache.hibernate.EhBlogTopic");
- System.out.println("查询到EhBlogTopic cache size:"
- + myCache1.getKeys().size());
- myCache1 = CacheManager.getInstance().getCache(
- "michael.cache.ehcache.hibernate.EhBlogTopic");
- System.out.println("EhBlogTopic cache size:"
- + myCache1.getKeys().size() + ",详细记录如下:");
- for (Object str : myCache1.getKeys()) {
- System.out.println(str);
- }
- System.out
- .println("在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql");
- EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
- 1);
- System.out.println("根据ID=1找到的记录:" + vo1);
- EhBlogTopic vo2 = new EhBlogTopic();
- vo2.setId(10);
- vo2.setUserId("michael");
- vo2.setTopic("Myblog:11");
- vo2.setSite("http://sjsky.iteye.com");
- session.save(vo2);
- ta.commit();
- System.out.println("新增加一条记录ID=10");
- } catch (Exception e) {
- e.printStackTrace();
- ta.rollback();
- }
- System.out.println("session closed.");
- System.out.println("====================================");
- Cache myCache1 = CacheManager.getInstance().getCache(
- "michael.cache.ehcache.hibernate.EhBlogTopic");
- System.out.println("EhBlogTopic cache size:"
- + myCache1.getKeys().size() + ",详细记录如下:");
- for (Object str : myCache1.getKeys()) {
- System.out.println(str);
- }
- System.out.println("====================================");
- System.out.println("在当前同一个sessionFactory,配置cache的bean");
- System.out.println("session open ....");
- try {
- Session session = sessionFactory.getCurrentSession();
- ta = session.beginTransaction();
- System.out
- .println("不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql");
- EhBlogTopic vo1 = (EhBlogTopic) session.get(EhBlogTopic.class,
- 1);
- System.out.println("根据ID=1找到的记录:" + vo1);
- System.out.println("查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql");
- EhBlogTopic vo2 = (EhBlogTopic) session.get(EhBlogTopic.class,
- 10);
- System.out.println("根据之前save的ID=10找到的记录:" + vo2);
- ta.commit();
- } catch (Exception e) {
- e.printStackTrace();
- ta.rollback();
- }
- System.out.println("session closed.");
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (null != sessionFactory) {
- sessionFactory.close();
- }
- }
- System.out.println("sessionFactory closed.");
- }
- }
运行结果如下:
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic2]; using defaults.
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic3]; using defaults.
buildSessionFactory===============
====================================
session open ....
同一个session(一级缓存默认的)中,没有配置cache的Bean
Hibernate: select ehuserinfo0_.ID as ID1_, ehuserinfo0_.USER_ID as USER2_1_, ehuserinfo0_.USER_NAME as USER3_1_, ehuserinfo0_.OHTER_INFO as OHTER4_1_ from MY_TB_EH_USER_INFO ehuserinfo0_
查询全部query.list().size:2
再根据ID=1查询某记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@63b2e6
session closed.
session open ....
第一次根据ID=1查询某记录时,会打印hsql
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@123a389
session closed.
====================================
当前同一个sessionFactory,没有配置cache的Bean,
session open ....
第一次根据ID=1查询记录时会再去查数据库,故打印hsql如下:
Hibernate: select ehuserinfo0_.ID as ID1_0_, ehuserinfo0_.USER_ID as USER2_1_0_, ehuserinfo0_.USER_NAME as USER3_1_0_, ehuserinfo0_.OHTER_INFO as OHTER4_1_0_ from MY_TB_EH_USER_INFO ehuserinfo0_ where ehuserinfo0_.ID=?
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhUserInfo@429c19
session closed.
====================================
缓存的key cacheNames length := 7
name := sampleCache2
name := michael.cache.ehcache.hibernate.EhBlogTopic2
name := org.hibernate.cache.UpdateTimestampsCache
name := sampleCache1
name := michael.cache.ehcache.hibernate.EhBlogTopic
name := org.hibernate.cache.StandardQueryCache
name := michael.cache.ehcache.hibernate.EhBlogTopic3
====================================
同一个session(一级缓存默认的)中,配置cache的Bean
session open ....
Hibernate: select ehblogtopi0_.ID as ID2_, ehblogtopi0_.USER_ID as USER2_2_, ehblogtopi0_.TOPIC as TOPIC2_, ehblogtopi0_.SITE as SITE2_ from MY_TB_EH_BLOG_TOPIC ehblogtopi0_
查询全部query.list().size:9
查询到EhBlogTopic cache size:9
EhBlogTopic cache size:9,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
在同一个session,再根据ID=1查询记录时不会再去查数据库,故不会打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@11a0d35
Hibernate: insert into MY_TB_EH_BLOG_TOPIC (USER_ID, TOPIC, SITE, ID) values (?, ?, ?, ?)
新增加一条记录ID=10
session closed.
====================================
EhBlogTopic cache size:10,详细记录如下:
michael.cache.ehcache.hibernate.EhBlogTopic#6
michael.cache.ehcache.hibernate.EhBlogTopic#5
michael.cache.ehcache.hibernate.EhBlogTopic#7
michael.cache.ehcache.hibernate.EhBlogTopic#8
michael.cache.ehcache.hibernate.EhBlogTopic#2
michael.cache.ehcache.hibernate.EhBlogTopic#9
michael.cache.ehcache.hibernate.EhBlogTopic#10
michael.cache.ehcache.hibernate.EhBlogTopic#1
michael.cache.ehcache.hibernate.EhBlogTopic#4
michael.cache.ehcache.hibernate.EhBlogTopic#3
====================================
在当前同一个sessionFactory,配置cache的bean
session open ....
不管是否第一次查询ID=1的记录,如果cache中存在的,则不会再查数据库,故不打印hsql
根据ID=1找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1321f5
查询之前session保存的数据ID=10,也不会再查数据库,故不打印hsql
根据之前save的ID=10找到的记录:michael.cache.ehcache.hibernate.EhBlogTopic@1a6518
session closed.
sessionFactory closed.
我们从上面的详细运行日志中就可以看出cache的效果。
本文连接:http://sjsky.iteye.com/blog/1312132
- Hibernate缓存深入详解.rar (1.9 MB)
- 下载次数: 247
- Spring事务详解.rar (626.9 KB)
- 下载次数: 103
评论
2011-12-15 13:27:15 net.sf.ehcache.hibernate.AbstractEhcacheProvider buildCache
警告: Could not find a specific ehcache configuration for cache named [michael.cache.ehcache.hibernate.EhBlogTopic]; using defaults.
缓存的名字<cache name="sampleCache1" ,不是随便起的,要写类全名,你那边报错就是指没有找到该类的缓存,使用默认的defaultCache了。
发表评论
-
java synchronized详解
2011-12-21 10:58 1046FROM: http://www.cnblogs.c ... -
Hibernate缓存与spring事务详解
2011-12-08 20:00 1131FROM http://www.iteye.com/topic ... -
hibernate文档 hibernate3中文文档
2011-11-22 14:19 1149如题 -
web.xml 通过contextConfigLocation配spring 的方式
2011-11-19 09:20 19053web.xml 通过contextconfiglo ... -
解决:Table flex.flex_user' doesn't exist
2011-11-17 09:48 1684Table flex.flex_user' doesn't e ... -
解决:为什么在生成hibernate逆向工程时不能选择spring Dao
2011-11-16 18:32 2655进入项目目录找到.springBeans ,用记事本 ... -
SSH单元测试代码整理
2011-11-03 13:12 1541package test; import java.ut ... -
SSH常见错误
2011-10-29 11:10 1764SSH常见错误Spring Hibernate在AOP方面的配 ... -
Attribute "lazy" with value "true" must have a value from the list "false proxy
2011-05-06 17:42 29942008年06月20日 星期五 上午 11:25 ... -
Hibernate配置文件在单元测试中的灵活运用
2008-12-26 15:59 1329对于hibernate的初学者来说,第一次使用hibernat ...
相关推荐
Hibernate+ehcache二级缓存技术 Hibernate+ehcache二级缓存技术
struts2+hibernate+ehcache二级缓存的 步骤和需要的代码及jar包
此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。
SSH三层架构MVC(struts1.3+spring2.x+hibernate3.2),Hibernate(ehcache)二级缓存技术,Spring 注解形式依赖注入,ehcache缓存 源代码,内有MySql anbyke.sql文件,方便创建数据库演示效果!
hibernate使用的一些例子,二级缓存ehcache的使用 一些设计模式的demo代码
Hibernate EhCache 二级缓存配置
配置 EhCache 的实例 1、 简介: EhCache缓存插件是Hibernate的另一个项目,Hibernate框架内置了对它 的支持,对于单机应用推荐使用它做为Hibernate的二级缓存。
hibernate4配置ehcache二级缓存,以及相关的jar都在源码中。
Hibernate中默认的二级缓存机制ehcache案例,属性ehcache环境搭建,配置问题。
ehcache 二级缓存 配置使用的jar包 配置如下: <!-- 启用二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true <!-- 查询的二级缓存配置 --> <property name="hibernate....
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
Hibernate4二级缓存Ehcache案例,可参考博客:http://blog.csdn.net/coco2d_x2014/article/details/52927638
hibernate二级缓存 ehcache
本人在做项目时用到了Hibernate的二级缓存,使用的是EhCache,结合本人自己的理解总结了如何在java web项目中配合Hibernate使用二级缓存,以提高程序的性能,附带需要的文件,参考的文件,和测试类以及说明。
使用Java工程实现了Hibernate二级缓存(Ehcache),其中还有自己封装的一个jar包实现了简单的分页功能
ehcache全解析 利用Spring和EHCache缓存结果 Hibernate+ehcache二级缓存技术 Spring基于注解的缓存配置--web应用实例 http://zjava.org.ru/
struts1.x+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,数据库mysql,需要自己创建
2.4. 使用EhCache配置二级缓存: 2.4.1. 配置准备: 1) 把ehcache-1.2.3.jar加入到当前应用的classpath中。 2) 在hibernate.cfg.xml文件中加入EhCache缓存插件的提供类。 <!--配置缓存插件 --> ...
Hibernate二级缓存+分页功能
hibernate二级缓存相关插件(EHcache)