【问题标题】:Hibernate 5 Second level Cache is not working , Still fetching from DatabaseHibernate 5 二级缓存不起作用,仍在从数据库中获取
【发布时间】:2019-02-08 11:03:16
【问题描述】:

我计划使用 Hibernate 5 实现二级缓存。Entity 类使用 @Cacheable 注释并添加策略为 CacheConcurrencyStrategy.READ_WRITE 。但它仍在寻找数据库而不是缓存中的数据。日志显示 2 个 SQL 查询。

请看我的主要方法

public static void main(String[] args) {
    HolidayDAOImpl holidayImpl = new HolidayDAOImpl();
    holidayImpl.setSessionFactory(HibernateUtil.INSTANCE.getSessionFactoryInstance());
    holidayImpl.load().forEach(System.out :: println);
    System.out.println("Loading second time");
    holidayImpl.load().forEach(System.out::println);
}   

请查看 HibernateUtil 枚举

public enum HibernateUtil{
INSTANCE;

public SessionFactory getSessionFactoryInstance(){  
    Properties properties = new Properties();
    properties.setProperty(Environment.URL, "jdbc:mysql://dummy");
    properties.setProperty(Environment.DIALECT, "org.hibernate.dialect.MySQLDialect");
    properties.setProperty(Environment.DRIVER, "com.mysql.jdbc.Driver");
    properties.setProperty(Environment.USER, "demo");
    properties.setProperty(Environment.PASS, "demo");

    //second level cache prop

    properties.setProperty(Environment.USE_SECOND_LEVEL_CACHE, "true");
    properties.setProperty(Environment.USE_QUERY_CACHE, "true");
    properties.setProperty(Environment.CACHE_REGION_FACTORY, "org.hibernate.cache.ehcache.EhCacheRegionFactory");

    //logging

    properties.setProperty("hibernate.show_sql","true");
    properties.setProperty("hibernate.format_sql","true");

    Configuration cfg = new Configuration();
    cfg.setProperties(properties);
    cfg.addAnnotatedClass(Holidays.class);
    StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties()).build();
    SessionFactory sessionFactory = cfg.buildSessionFactory(serviceRegistry);
    return sessionFactory;
}
}

请查看 DAOImpl 类

public List<Holidays> load() {
    try (Session session = sessionFactory.openSession()) {

        //List<Holidays> result = session.createQuery("from Holidays", Holidays.class).getResultList();
        Criteria criteria  = session.createCriteria(Holidays.class);
        criteria.add(Restrictions.like("holiday_name", "%QA%"));

        return criteria.list();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

pom.xml

<dependencies>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.13</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>5.4.1.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-ehcache</artifactId>
        <version>5.4.1.Final</version>
    </dependency>
</dependencies>

在浏览一些堆栈溢出的答案时,可以理解在最新版本的 Hibernate(版本 4 之后)中,配置有一些变化。不确定我在配置中犯了什么错误。

任何人都可以查看它,并弄清楚为什么它仍在寻找数据库而不是缓存?

【问题讨论】:

    标签: java hibernate ehcache


    【解决方案1】:

    要使用休眠查询缓存,您必须在每个查询中添加setCacheable (true)

    如果将criteria.setCacheable (true); 添加到DAOImpl 类中,则第二个查询结果来自缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-24
      • 2010-11-29
      • 1970-01-01
      • 2010-10-16
      • 2021-10-07
      • 1970-01-01
      • 2015-05-14
      • 1970-01-01
      相关资源
      最近更新 更多