`

hibernate 缓存 初试

阅读更多

配置二级缓存

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!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="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

  <property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
  <property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
  <property name="hibernate.connection.url">jdbc:jtds:sqlserver://localHost:1433/DSO</property>
  <property name="hibernate.connection.username">sa</property>
  <property name="hibernate.connection.password">sa</property>
  <property name="show_sql">false</property>

  <mapping resource="ComPermission.hbm.xml" />
 </session-factory>
</hibernate-configuration>

 ehcache.xml

<ehcache>
         <diskStore path="java.io.tmpdir" />
         <defaultCache
            maxElementsInMemory="10000"
            eternal="false"             
            timeToIdleSeconds="1200"     
            timeToLiveSeconds="1200"     
            overflowToDisk="true"       
         />
    </ehcache>

 

ComPermission.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
 <class name="ComPermission" table="JB_COM_PERMISSION" dynamic-insert="true" dynamic-update="true">
  <cache usage="read-write" include="all" />
  <id name="permissionId" type="java.lang.String">
   <column name="PERMISSION_ID" length="5" />
   <generator class="assigned"></generator>
  </id>
  <property name="permissionCode" type="java.lang.String">
   <column name="PERMISSION_CODE" length="5" not-null="true" />
  </property>
  <property name="permissionName" type="java.lang.String">
   <column name="PERMISSION_NAME" length="30" not-null="true" />
  </property>
  <property name="remark" type="java.lang.String">
   <column name="REMARK" length="255" />
  </property>
 </class>
</hibernate-mapping>

 Java实体类 ComPermission

public class ComPermission implements java.io.Serializable {

    // Fields

    private String permissionId;
    private String permissionCode;
    private String permissionName;
    private String remark;

}

 log4j设置

log4j.category.org.hibernate.cache=DEBUG,CA
log4j.category.org.hibernate.SQL=DEBUG,CA

 

测试代码

public static void main(String[] args) {
	    System.out.println(HibernateSessionFactory.getConfiguration());   
        System.out.println("|||||||||||||||||||||||||||||||||||||||||||");   
        Session session = HibernateSessionFactory.getSessionFactory().openSession();   
        long start = System.currentTimeMillis();
        try {   
            ComPermission permission = (ComPermission) session.load(ComPermission.class, "10001");   
            System.out.println(permission.getPermissionName());   
        } catch (RuntimeException re) {   
            throw re;   
        } finally {   
            session.close();   
        }   
        System.out.println("time :" + (System.currentTimeMillis() - start));   
        System.out.println("-----------------------------------------------");   
  
        Transaction tran = null;   
        Session session2 = HibernateSessionFactory.getSessionFactory().openSession();   
        start = System.currentTimeMillis();
        try {   
            tran = session2.beginTransaction();   
            ComPermission permission = (ComPermission) session2.load(ComPermission.class, "10001");   
            permission.setPermissionName(permission.getPermissionName() + "!");   
            System.out.println(permission.getPermissionName());   
            session2.update(permission);   
            tran.commit();   
        } catch (RuntimeException re) {   
            tran.rollback();   
            throw re;   
        } finally {   
            session2.close();   
        }   
        System.out.println("time :" + (System.currentTimeMillis() - start));   
        System.out.println("---------------------------------------------------------");   
  
        
        Session session3 = HibernateSessionFactory.getSessionFactory().openSession();   
        start = System.currentTimeMillis();   
        try {   
            ComPermission permission = (ComPermission) session3.load(ComPermission.class, "10001");   
            System.out.println(permission.getPermissionName());   
        } catch (RuntimeException re) {   
            throw re;   
        } finally {   
            session3.close();   
        }   
        System.out.println("time :" + (System.currentTimeMillis() - start));   
        System.out.println("---------------------------------------------------------");   
  
        
        Session session4 = HibernateSessionFactory.getSessionFactory().openSession();   
        start = System.currentTimeMillis();   
        try {   
            ComPermission permission = (ComPermission) session4.load(ComPermission.class, "10001");   
            System.out.println(permission.getPermissionName());   
        } catch (RuntimeException re) {   
            throw re;   
        } finally {   
            session4.close();   
        }   
        System.out.println("time :" + (System.currentTimeMillis() - start));   
    } 

 

log信息

DEBUG  [org.hibernate.cache.CacheFactory.createCache:39]  instantiating cache region: ComPermission usage strategy: read-write
WARN  [org.hibernate.cache.EhCacheProvider.buildCache:103]  Could not find configuration [ComPermission]; using defaults.
DEBUG  [org.hibernate.cache.EhCacheProvider.buildCache:106]  started EHCache region: ComPermission
org.hibernate.cfg.Configuration@c4fe76
|||||||||||||||||||||||||||||||||||||||||||
DEBUG  [org.hibernate.cache.ReadWriteCache.get:75]  Cache lookup: ComPermission#10001  //尝试在缓存中查找对象  ComPermission#10001  
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:113]  Element for ComPermission#10001 is null
DEBUG  [org.hibernate.cache.ReadWriteCache.get:91]  Cache miss: ComPermission#10001    //找不到
DEBUG  [org.hibernate.jdbc.AbstractBatcher.log:346]  select compermiss0_.PERMISSION_ID as PERMISSION1_0_0_, compermiss0_.PERMISSION_CODE as PERMISSION2_0_0_, compermiss0_.PERMISSION_NAME as PERMISSION3_0_0_, compermiss0_.REMARK as REMARK0_0_ from JB_COM_PERMISSION compermiss0_ where compermiss0_.PERMISSION_ID=?
DEBUG  [org.hibernate.cache.ReadWriteCache.put:148]  Caching: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:113]  Element for ComPermission#10001 is null
DEBUG  [org.hibernate.cache.ReadWriteCache.put:160]  Cached: ComPermission#10001      //将查询到的缓存起来
测试信息
time :47
-----------------------------------------------
DEBUG  [org.hibernate.cache.ReadWriteCache.get:75]  Cache lookup: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.cache.ReadWriteCache.get:85]  Cache hit: ComPermission#10001     //找到了
测试信息!
DEBUG  [org.hibernate.cache.ReadWriteCache.lock:113]  Invalidating: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.jdbc.AbstractBatcher.log:346]  update JB_COM_PERMISSION set PERMISSION_NAME=? where PERMISSION_ID=?
DEBUG  [org.hibernate.cache.ReadWriteCache.release:195]  Releasing: ComPermission#10001  //由于对象被更新 将缓存中的对象释放
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
time :31
---------------------------------------------------------
DEBUG  [org.hibernate.cache.ReadWriteCache.get:75]  Cache lookup: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.cache.ReadWriteCache.get:94]  Cached item was locked: ComPermission#10001  //lockable.isGettable(txTimestamp)==false
DEBUG  [org.hibernate.jdbc.AbstractBatcher.log:346]  select compermiss0_.PERMISSION_ID as PERMISSION1_0_0_, compermiss0_.PERMISSION_CODE as PERMISSION2_0_0_, compermiss0_.PERMISSION_NAME as PERMISSION3_0_0_, compermiss0_.REMARK as REMARK0_0_ from JB_COM_PERMISSION compermiss0_ where compermiss0_.PERMISSION_ID=?
DEBUG  [org.hibernate.cache.ReadWriteCache.put:148]  Caching: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.cache.ReadWriteCache.put:160]  Cached: ComPermission#10001   //重新缓存对象
测试信息!
time :0
---------------------------------------------------------
DEBUG  [org.hibernate.cache.ReadWriteCache.get:75]  Cache lookup: ComPermission#10001
DEBUG  [org.hibernate.cache.EhCache.get:104]  key: ComPermission#10001
DEBUG  [org.hibernate.cache.ReadWriteCache.get:85]  Cache hit: ComPermission#10001     //缓存命中
测试信息!
time :0

 

分享到:
评论

相关推荐

    Hibernate缓存.doc

    Hibernate缓存.docHibernate缓存.doc

    Hibernate缓存深入详解 from ITEye

    **Hibernate缓存深入详解** 在Java企业级应用开发中,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。然而,随着应用规模的扩大,数据访问性能成为了一个不可忽视的问题。这时,...

    Hibernate缓存技术研究

    ### Hibernate缓存技术研究 #### 一、引言 Hibernate是一种强大的对象-关系映射(Object-Relational Mapping,简称ORM)工具,主要用于Java环境下的应用程序。它能够将应用程序中的对象模型映射到关系型数据库的表...

    Java Hibernate缓存深入详解

    Java Hibernate缓存深入详解

    hibernate缓存机制

    Hibernate缓存机制是提高应用程序性能的关键技术之一,它通过存储数据副本减少对物理数据库的访问。缓存可以分为两层:第一级缓存和第二级缓存。 **第一级缓存**是内置在Session中的,它是不可卸载的,也称为...

    hibernate缓存深入详解.

    关于hibernate缓存的一个ppt课件,60+幻灯片,有需要的可以看一下

    Hibernate缓存,性能优化

    在IT领域,尤其是在Java开发中,Hibernate作为一款流行的开源对象关系映射(ORM)框架,其缓存机制是实现高性能数据库交互的关键技术之一。本文将深入探讨Hibernate缓存的原理、类型及其对性能优化的影响。 ### ...

    Hibernate缓存深入详解

    【Hibernate缓存深入详解】 在Java的持久化框架Hibernate中,缓存机制是提升系统性能的关键因素。它位于Hibernate应用和数据库之间,减少了对数据库的直接访问,从而提高了应用程序的运行速度。缓存中存储的是...

    Hibernate缓存与spring事务详解

    **标题:“Hibernate缓存与Spring事务详解”** 在IT领域,尤其是Java开发中,Hibernate作为一款流行的ORM(对象关系映射)框架,极大地简化了数据库操作。而Spring框架则以其全面的功能,包括依赖注入、AOP(面向切...

    Hibernate缓存策略

    Hibernate缓存原理及调优策略 Hibernate缓存原理调优策略

    Hibernate缓存详解

    **Hibernate缓存详解** 在Java开发中,Hibernate作为一款强大的对象关系映射(ORM)框架,极大地简化了数据库操作。为了提高数据访问性能,Hibernate引入了缓存机制,它可以减少对数据库的直接访问,从而提升应用的...

    hibernate缓存

    ### Hibernate缓存机制及优化策略 #### 一、概述 Hibernate作为一款优秀的对象关系映射(ORM)框架,在Java开发领域被广泛应用于数据库操作。它提供了丰富的缓存机制来提高应用性能并降低数据库访问压力。本文将...

    hibernate缓存和事务

    Hibernate 提供了两级缓存机制:第一级缓存和第二级缓存。 1. **第一级缓存**:这是SessionFactory级别的缓存,每个SessionFactory实例都有自己的第一级缓存。当对象被持久化或者从数据库加载时,它们会被放入第一...

    hibernate缓存ehcache用法

    Ehcache是Hibernate常用的二级缓存解决方案,它可以提高应用程序的性能和响应速度。这篇博客文章“hibernate缓存ehcache用法”可能详细介绍了如何在Hibernate中配置和使用Ehcache。 首先,我们需要理解什么是缓存。...

    Hibernate教程25_Hibernate缓存

    **标题解析:** "Hibernate教程25_Hibernate缓存" 这个标题表明了我们要讨论的是关于Hibernate框架的第25个教程,重点是它的缓存机制。Hibernate是一个流行的Java对象关系映射(ORM)框架,它允许开发者用面向对象的...

    Hibernate缓存深入讲解

    关于hibernate缓存的讲解,以及缓存的优化。讲的非常透彻,对于项目中Hibernate缓存的优化会有很大帮助!

Global site tag (gtag.js) - Google Analytics