Question

I have tried to enable query cache without luck in JBoss 7.1.1 I have added this in the code:

TypedQuery<Currency> query = entityManager.createNamedQuery("getCurrency",Currency.class);
query.setParameter("code", code);
query.setHint("javax.persistence.cache.storeMode", CacheStoreMode.REFRESH);
query.setHint("javax.persistence.cache.retrieveMode", CacheRetrieveMode.USE);
query.setHint("org.hibernate.cacheable", true); 

The namedQuery looks like this:

@Cacheable
@Entity
@Table(name = "currency")
@NamedQuery(
    name = "getCurrency",
    query = "FROM Currency c WHERE c.iso4217code = :code"
)

I have in my persistence.xml the following:

<persistence-unit name="cache_persistence">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>

    <jta-data-source>java:jboss/datasources/cache</jta-data-source>
    <class>com.unwire.cache.model.CacheTest</class>
    <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
    <properties>
     <property name="hibernate.show_sql" value="false" />
     <property name="hibernate.hbm2ddl.auto" value="none" />
     <property name="hibernate.cache.use_second_level_cache" value="true"/>
     <property name="hibernate.cache.use_query_cache" value="true"/>
     <property name="hibernate.generate_statistics" value="true"/>
    </properties>

</persistence-unit>

When I run the war file on the server The entity gets cached but the query cache is never used

I have uploaded the file here: http://www.filedropper.com/cachetest

Was it helpful?

Solution

In my code (ear archive, but this should not make a difference) I simply have:

query.setHint("org.hibernate.cacheable", true); 
query.setHint("org.hibernate.cacheMode", "NORMAL"); 

and

<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
  <properties>
  <property name="hibernate.cache.use_second_level_cache" value="true"/>
  <property name="hibernate.cache.use_query_cache" value="true"/>
  <property name="hibernate.generate_statistics" value="true" />
</properties>

and without any additional things (like you have) and it works. Of course you must test the caching against the same parameters for the query.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top