Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EhCache + hibernate

I have the following problem: I have a query which return me 35 results and I would like to keep in second level cache:

public List<Product> getAllProducts() {
        Session session = this.sessionfactory.getCurrentSession();
        String queryString = "from com.ewave.upromotions.objects.Product product where product.active=:active";
        Query query = session.createQuery(queryString); 
        query.setBoolean("active", true);
        query.setCacheable(true);
        query.setCacheRegion("productCache");
        List<Product> products =query.list();
        return products;
    }

My object is the following:

@Entity
@Table(name="products",schema="test11")
@Cacheable
@Cache( usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class Product implements Serializable {

//all setters and getters ommited:

}

my ehcache.xml file is in /src/ directory:

<ehcache>  
    <diskStore path="java.io.tmpdir"/>  
    <defaultCache maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="false"/>  
    <cache name="hibernate.test.org.hibernate.cache.UpdateTimestampsCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>  
    <cache name="hibernate.test.org.hibernate.cache.StandardQueryCache"   
        maxElementsInMemory="10000"  
        eternal="false"  
        timeToIdleSeconds="120"  
        timeToLiveSeconds="120"  
        overflowToDisk="true"/>
     <cache name="com.vanilla.objects.Product"
        maxElementsInMemory="300"
        eternal="false"
        overflowToDisk="false"
        timeToIdleSeconds="600"  
        timeToLiveSeconds="600"  
        />  
       <cache name="productCache" 
       maxElementsInMemory="100" 
       eternal="true" 
       overflowToDisk="false" />

</ehcache>  

and my Hibernate configuration is:

<props>
    <prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop> 
    <prop key="hibernate.cache.use_second_level_cache">true</prop> 
     <prop key="hibernate.show_sql">true</prop> 
  <prop key="hibernate.cache.use_query_cache">true</prop> 
  <prop key="hibernate.cache.provider_class">net.sf.ehcache.hibernate.SingletonEhCacheProvider</prop>
  <prop key="hibernate.connection.release_mode">after_transaction</prop>
  <prop key="hibernate.cache.provider_configuration_file_resource_path">/ehcache.xml</prop>
  </props>

My problem is the following: when bringing page for first time I see the select which brings all 35 results:

when I refresh page, instead of bringing 35 objects from cache I see 35 select statements which queries objects by id one by one.

What's wrong?

like image 330
danny.lesnik Avatar asked Jul 17 '11 13:07

danny.lesnik


People also ask

What is Ehcache in Hibernate?

Ehcache as a plug-in second-level cache for Hibernate – Automatically cache common queries in memory to substantially lower latency. BigMemory for an in-memory store – Leverage off-heap physical memory to keep more of the data set close to your application and out of reach of Java garbage collection.

Which cache is best for Hibernate?

Hibernate 2nd level cache is best suitable for data that rarely or never changes. However since hibernate provides a generalized caching provider implemented by multiple caching solutions, it limits usability of features provided by caching solutions.

How do I enable Ehcache?

Enabling EhCache To configure ehcache, we need to do two steps: Configure Hibernate to enable second-level caching. Specify the second-level cache provider as ehcache.

Which 2nd level cache is better in Hibernate?

Hibernate second level cache uses a common cache for all the session object of a session factory. It is useful if you have multiple session objects from a session factory. SessionFactory holds the second level cache data. It is global for all the session objects and not enabled by default.


1 Answers

It was happening to me when query was cached but objects were not. In you sample code I see that query refers to com.ewave.upromotions.objects.Product but you have cache region defined for com.vanilla.objects.Product. And you didn't provide any cache region to the Product class. So I'd suggest that you explicitly specify cache region for Product and define it in cache configuration. To start, I would try the same region for query and object, just to see if it works, and then try separate configurations.

like image 65
Alex Gitelman Avatar answered Sep 21 '22 03:09

Alex Gitelman