Question

I've set up ehcache on our Java application, which uses Spring and Hibernate. However, when I run Junit tests and print the stats, it seems there is nothing in cache:

OUTPUT OF CACHE MANAGER STATS ON EVERY CACHE:

COM.****.SERVICES.CLARITY.DOMAIN.ACTIONITEM.BYRESOURCEUNIQUENAME: getCacheHits: 0 getCacheMisses: 0 getObjectCount: 0

COM.****.SERVICES.CLARITY.DOMAIN.ACTIONITEM: getCacheHits: 0 getCacheMisses: 0 getObjectCount: 0

COM.****.SERVICES.CLARITY.DOMAIN.RESOURCE: getCacheHits: 0 getCacheMisses: 0 getObjectCount: 0

CONTENT OF THE MAPPING FILE (ONLY PARTS, TOO BIG TO PASTE ALL):

<class name="ActionItem" table="CAL_ACTION_ITEMS" mutable="false" lazy="false" >
<cache region="com.****.services.clarity.domain.ActionItem" usage="read-only" include="all" />

[...]

<query name="byResourceUniqueName" cacheable="true" cache-region="com.****.services.clarity.domain.ActionItem.byResourceUniqueName" read-only="true">
    FROM ActionItem WHERE id IN (
        SELECT DISTINCT actionItemId FROM ActionItemAssignee as aia WHERE assigneeId IN (
            SELECT userId FROM Resource WHERE uniqueName = :uniqueName
        )
    )
    ORDER BY dueDate
</query>

CONTENT OF EHCACHE.XML:

<cache
    name="com.****.services.clarity.domain.ActionItem"
    maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="0"
    timeToLiveSeconds="600" overflowToDisk="false" />

<cache
    name="com.****.services.clarity.domain.ActionItem.byResourceUniqueName"
    maxElementsInMemory="2000" eternal="false" timeToIdleSeconds="0"
    timeToLiveSeconds="60" overflowToDisk="false" />

<defaultCache maxElementsInMemory="200" eternal="false"
    timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
    memoryStoreEvictionPolicy="LRU" />

HIBERNATE CONFIG:

    <property name="hibernateProperties">
        <value>
            hibernate.format_sql=true
            hibernate.dialect=org.hibernate.dialect.OracleDialect
            hibernate.cache.provider_class=net.sf.ehcache.hibernate.EhCacheProvider
            hibernate.cache.use_query_cache=true
            hibernate.show_sql=true
        </value>
    </property>
</bean>

Any ideas on how to populate the cache ? Thanks.

Was it helpful?

Solution

Normally the items get populated to the cache by Hibernate automatically.

One thing I noticed in your configuration is that you didn't enable the statistics. Add the property

hibernate.generate_statistics=true

to your session factory's configuration and see, if numbers occur in your output.

OTHER TIPS

yeah,when you use second cache in hibernate , when you will query you must set like this:

Query q=....
q.setCacheable(tre);

if you Spring you must write like this

getHibernateTemplate().setCacheQueries(true);

First of all, are you sure cache usage for ActionItems should be read-only? How are the those items populated initially? And, even if that is correct for your business model, consider changing it to nonstrict-read-write, to see if it changes anything in your test.

Then, have you tried executing byResourceUniqueName query with caching enabled:

Query q = getSession().getNamedQuery("byResourceUniqueName");
q.setCacheable(true);
List result = q.list();

Even though cacheable parameter should influence only caching of the result set of the query, again, see if it changes anything.

Try using the net.sf.ehcache.hibernate.SingletonEhCacheProvider instead of the net.sf.ehcache.hibernate.EhCacheProvider

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