Hibernate 5 二级缓存不工作,仍在从数据库中获取
Hibernate 5 Second level Cache is not working , Still fetching from Database
我计划用 Hibernate 5 实现二级缓存。实体 class 用 @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 class
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 之后)中,配置发生了一些变化。不确定我在配置中是否有任何错误。
谁能调查一下,弄清楚为什么它仍在寻找数据库而不是缓存?
要使用 hibernate 查询缓存,您必须在每个查询中添加 setCacheable (true)
。
如果在DAOImplclass中添加criteria.setCacheable (true);
,则第二个查询结果来自缓存。
我计划用 Hibernate 5 实现二级缓存。实体 class 用 @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 class
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 之后)中,配置发生了一些变化。不确定我在配置中是否有任何错误。
谁能调查一下,弄清楚为什么它仍在寻找数据库而不是缓存?
要使用 hibernate 查询缓存,您必须在每个查询中添加 setCacheable (true)
。
如果在DAOImplclass中添加criteria.setCacheable (true);
,则第二个查询结果来自缓存。