EhCache + sleep mode

I have the following problem: I have a query that returns 35 results for me, and I would like to cache in the second level:

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 as follows:

 @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 located in the / 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:

 <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 this: when I cast the page for the first time, I see a choice that brings all 35 results:

when I refresh the page, instead of displaying 35 objects from the cache, I see 35 select statements that alternately request objects by identifier.

What happened?

+6
source share
1 answer

This happened to me when the request was cached, but there were no objects. In your code example, I see that the request refers to com.ewave.upromotions.objects.Product , but you have a cache area defined for com.vanilla.objects.Product . And you did not provide the cache area to the Product class. Therefore, I suggest that you explicitly specify the cache area for Product and define it in the cache configuration. To get started, I would try the same region for the query and the object, just to make sure it works, and try the individual settings.

+5
source

Source: https://habr.com/ru/post/892935/


All Articles