Ansar Samad
Ansar Samad

Reputation: 602

Hibernate 5 Second level Cache is not working , Still fetching from Database

Am planning to implement second level cache with Hibernate 5 .Entity class is annotated with @Cacheable and added strategy as CacheConcurrencyStrategy.READ_WRITE . but its still loooking for data in databse instead of cache .Logs shows 2 SQL queries.

Please see my main method

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

Please see the HibernateUtil enum

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;
}
}

Please see the 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>

While going through some of the stack overflow answers, could understand that in latest version of Hibernate (after version 4), there are some changes in configurations. Not sure i made any mistake in the configurations.

Could anyone please look into it, and figure out why its still looking for database instead of cache?

Upvotes: 0

Views: 1527

Answers (1)

aburakc
aburakc

Reputation: 109

To use hibernate query cache, you must add setCacheable (true) in each query.

If you add criteria.setCacheable (true); to the DAOImpl class, the second query result comes from the cache.

Upvotes: 2

Related Questions