Hibernate+EhCache配置二级缓存

   blog迁移至 :http://www.micmiu.com

 

本文主要讲一讲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中标签配置方式:

        (3).映射文件*.hb.xml中标签配置方式: 1. classpath:ehcahce.xml配置文件如下:

 




    

    

    

    

    

    

    

    

    2.hibernate配置文件:hibernate.cfg.xml





	
		
			oracle.jdbc.driver.OracleDriver
		
		
			jdbc:oracle:thin:@localhost:1521:ora11g
		
		mytest
		111111

		
			org.hibernate.dialect.Oracle9Dialect
		

		true
		UTF-8
		true
		10
		10


		true
		false
		thread
		update
		
		
		
		
		
			net.sf.ehcache.hibernate.SingletonEhCacheProvider
		
		
		
		
			true
		
		
			true
		

		
		
		
		

		
		

		
		

	

   3.相关javabean代码片段如下:

      (1).hibernate.cfg.xml中标签配置的: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






	
		
		
			
		
		

		
		

	

      (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.");
    }
}

 运行结果如下:

 

ehcache - hibernate Test ...
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:


Hibernate+EhCache配置二级缓存_第1张图片

  EhBlogTopic:
Hibernate+EhCache配置二级缓存_第2张图片

   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.");
    }
}
 

 运行结果如下:

 

ehcache - hibernate Test ...
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

 

 

 

转载请注明来自:Michael's blog @ http://sjsky.iteye.com



----------------------------- 分 ------------------------------ 隔 ------------------------------ 线 ------------------------------

 

 

 

你可能感兴趣的:(Hibernate)