Nathan
Nathan

Reputation: 1478

Hazelcast Hibernate second-level cache configuration

I'm experimenting with using Hazelcast as the second-level cache for Hibernate. I'm unsure where to specify the cache configuration. I've added the following to my Hazelcast XML:

<map name="com.blah.entity.*">
    <backup-count>1</backup-count>
    <time-to-live-seconds>3600</time-to-live-seconds>
    <max-idle-seconds>600</max-idle-seconds>
    <eviction-policy>LRU</eviction-policy>
    <max-size policy="PER_NODE">5</max-size>
    <eviction-percentage>25</eviction-percentage>
    <near-cache>
        <max-size>5</max-size>
        <time-to-live-seconds>3600</time-to-live-seconds>
        <max-idle-seconds>600</max-idle-seconds>
        <eviction-policy>LRU</eviction-policy>
        <invalidate-on-change>true</invalidate-on-change>
    </near-cache>
</map>

However, when I use JVisualVM (with JMX plugin) to view the Map MBean, I see that the size of the map is 21 (which is larger than my max size of 5).

The maps 'config' attribute on the MBean shows that my configuration has been applied:

MapConfig{name='com.blah.entity.*',
  inMemoryFormat=BINARY',
  backupCount=1,
  asyncBackupCount=0,
  timeToLiveSeconds=3600,
  maxIdleSeconds=600,
  evictionPolicy='LRU',
  evictionPercentage=25,
  maxSizeConfig=MaxSizeConfig{maxSizePolicy='PER_NODE',
    size=5},
  readBackupData=false,
  nearCacheConfig=NearCacheConfig{timeToLiveSeconds=3600,
    maxSize=5,
    evictionPolicy='LRU',
    maxIdleSeconds=600,
    invalidateOnChange=true,
    inMemoryFormat=BINARY,
    cacheLocalEntries=false},
  mapStoreConfig=null,
  mergePolicyConfig='com.hazelcast.map.merge.PutIfAbsentMapMergePolicy',
  wanReplicationRef=null,
  listenerConfigs=[],
  mapIndexConfigs=[]}

Am I doing something wrong in the config, or am I misreading the JMX data?

EDIT

Just to clear up any confusion, the documentation says the following:

Hazelcast creates a separate distributed map for each Hibernate cache region. So, these regions can be configured easily via Hazelcast map configuration. You can define backup, eviction, TTL and Near Cache properties.

I want to set the max-size property on the maps for the cache regions for my entities because I don't want the cache to expand without limit.

Upvotes: 2

Views: 2966

Answers (2)

Nathan
Nathan

Reputation: 1478

It turns out that my configuration is correct, but there is a bug in Hazelcast 3.2.1 - 3.2.4 for Wildcard Configuration

The problem is the wildcard in the map name: <map name="com.blah.entity.*"> If I use a fully qualified name for the map, e.g. com.blah.entity.User, the map configuration is picked up correctly. I verified using JMX that the map configuration is applied to the cache correctly.

I've been assured that this issue will be fixed in Hazelcast 3.3

Upvotes: 1

noctarius
noctarius

Reputation: 6094

You specify the path to the configuration using the following property inside your Hibernate configuration hibernate.cache.hazelcast.configuration_file_path.

Upvotes: 0

Related Questions