tb0171
tb0171

Reputation: 21

Cache region configurations in persistence.xml

According to this documentation, when using Infinispan with Hibernate it is possible to define multiple cache regions and configure them individually. I'm using WildFly 8.0, which uses Infinispan as a default 2LC provider for Hibernate. So here's my standalone.xml configuration for hibernate cache container:

<cache-container name="hibernate" default-cache="local-query" module="org.hibernate">
    <local-cache name="entity">
        <transaction mode="NON_XA"/>
        <eviction strategy="LRU" max-entries="10000"/>
        <expiration max-idle="100000"/>
    </local-cache>
    <local-cache name="local-query">
        <transaction mode="NONE"/>
        <eviction strategy="LRU" max-entries="10000"/>
        <expiration max-idle="100000"/>
    </local-cache>
    <local-cache name="timestamps">
        <transaction mode="NONE"/>
        <eviction strategy="NONE"/>
    </local-cache>
</cache-container>

So, the max idle expiration time is set to 100 seconds. This is how I tell Hibernate to cache query:

slotQuery.setHint("org.hibernate.cacheable", true)
         .setHint("org.hibernate.cacheRegion", "myRegionName")
         .getResultList();

Everything is working perfeclty, the queries are being cached for 100 seconds. But when I'm trying to configure a separate cache region for the query in persistence.xml:

<property name="hibernate.cache.use_second_level_cache" value="true"/>
<property name="hibernate.cache.use_query_cache" value="true"/>
<property name="hibernate.cache.infinispan.myRegionName.expiration.max_idle" value="5000"/>

settings are not being overriden, and the query is cached for 100 seconds. What am I missing here?

Upvotes: 2

Views: 2450

Answers (2)

ulab
ulab

Reputation: 1089

As mentioned by Galder Zamarreño in the comments, the deployment name has to be added to the region.

Or, you can explicitly set prefix to empty ("") or some value like below.

<property name="hibernate.cache.region_prefix" value="my-cache"/> 

And then specify the region prefix as below.

<property name="hibernate.cache.infinispan.my-cache.myRegionName.expiration.max_idle" value="5000"/>

Upvotes: 1

Tiago PC
Tiago PC

Reputation: 129

Make sure that you put in your persistence.xml the factory_class:

<property name="hibernate.cache.region.factory_class"
            value="org.hibernate.cache.infinispan.InfinispanRegionFactory" />

The value 5000 is really what you want ? Remember that this is in milliseconds, so your cache max idle is set to 5 seconds.

Regarding the cacheRegion, i couldn't find a way to create one costumized for queries. It is possible to overwrite the infinispan default though. You could use the default query cache region to configure the max_idle, this way:

<!-- entity cache, never expires = -1 -->
<property name="hibernate.cache.infinispan.entity.expiration.max_idle"
            value="-1" />
<!-- query cache, with 12 hours expiration -->
<property name="hibernate.cache.infinispan.query.expiration.max_idle"
            value="1620000" />

Upvotes: 0

Related Questions