xiayh04 2015-10-19 09:16 采纳率: 0%
浏览 1509

Hibernate 二级缓存问题

框架是spring+hibernate,配置了二级缓存;
第一种方法二级缓存生效:直接调用session。
第二种方法二级缓存无效:使用hibernateTemplate。
方法1:(调用两次以下方法,第二次没有打印sql)

        s = sessionFactory.openSession(); 
        tx = s.beginTransaction(); 
        log.info("1 try to load application dsg."); 
        ApplicationDSG adsg = (ApplicationDSG) s.get(ApplicationDSG.class, appDSGId); 
        log.info(" --application dsg:" + adsg.toString()); 

方法2:使用hibernateTemplate

    hibernateTemplate.setCacheQueries(true); 
    ApplicationDSG adsg1 = hibernateTemplate.get(ApplicationDSG.class, appDSGId); 
    Application a1 = adsg1.getApplication(); 
    a1.getApplicationName(); 
    ApplicationDSG adsg2 = hibernateTemplate.get(ApplicationDSG.class, appDSGId); 
    Application a2 = adsg2.getApplication(); 
    a2.getApplicationName(); 

    System.out.println(hibernateTemplate.getQueryCacheRegion()); 

    Statistics statistics = sessionFactory.getStatistics(); 
    System.out.println(statistics); 
    System.out.println("放入" + statistics.getSecondLevelCachePutCount()); 
    System.out.println("命中" + statistics.getSecondLevelCacheHitCount()); 
    System.out.println("错过" + statistics.getSecondLevelCacheMissCount()); 

hibernateTemplate 在xml中生成:

 <!-- hibernate template -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory" ref="sessionFactory" />
    <property name="cacheQueries">
        <value>${hibernate.template.cacheQueries}</value>
    </property>
</bean>

 方法1,2中的sessionFactory是一个。 问题不在这方面。

有没有知道这个是什么原因的?希望给与解答。

  • 写回答

4条回答 默认 最新

  • Robot-C 2015-10-19 09:22
    关注

    相关概念和定义
    1、缓存的意义
    把一些不常修改,但是又经常用的数据存放到内存中,这样能减少与数据库的交互,提升程序的性能
    2、Hibernate中提供了两级缓存:
    第一级别的缓存是Session级别的缓存(比如说在调用get方法的时候,如果已经查询过一次了,第二次就不会查了,而是直接返回session缓存中已经存在的那个对象给你,不过这个只对当前Session有效,一旦又开一个新的Se......
    答案就在这里:Hibernate二级缓存问题
    ----------------------你好,人类,我是来自CSDN星球的问答机器人小C,以上是依据我对问题的理解给出的答案,如果解决了你的问题,望采纳。

    评论

报告相同问题?

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多