Hazelcast as Hibernate 2nd Level Cache

363 views
Skip to first unread message

FooDog

unread,
Aug 24, 2010, 3:15:22 AM8/24/10
to Hazelcast
Hey everyone,

I'm trying to get both to work with each other but I'm not there yet.

I have tried two test cases.
1. Using Hazelcast as the 2nd level cache.
- created hazelcast.xml file with <map> elements (name = entity-class
region) and defined everything as I need it to be
- used the @Cache(usage = ..., region = ...) annotation in my entity-
classes
- told hibernate to use_second_level_cache, defined the provider
(hibernate pre3.3), usenativeclient,nativeclienthosts,
nativeclientgroup and nativeclientpassword

The cache gets created, distributed to my hazelcast servers, objects
get populated and after the app shutsdown the cache is cleared.

2. Using the settings as shown above. Only exception is that I create
a cache manager that puts my objects in the cache
(myHazelcastClient.getMap(region from the entity-class).put(key,
object)) after beeing persisted with hibernate.

What happens now is that the cache gets created, object get populated
and distributed the the hazelcast servers, the app ends and ... the
cache is still there as I would expect it.

I'm monitoring both cases with the hazelcast-monitor webapp.

I'm using hibernate 3.2.6.ga, hazelcast 1.8.5 (also the ...-client.jar
and ...-hibernate.jar) and spring 2.5.6 and configuring hibernate with
a org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean
bean.

I'm not sure what is going wrong here, so it would be cool if someone
could give me a hint.

hazelcast.xml
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast>
<map name="articleCache">
<backup-count>1</backup-count>
<time-to-live-seconds>6000</time-to-live-seconds>
<max-idle-seconds>300</max-idle-seconds>
<max-size>10000</max-size>
<eviction-policy>LRU</eviction-policy>
<eviction-percentage>25</eviction-percentage>
<eviction-delay-seconds>3</eviction-delay-seconds>
</map>
</hazelcast>

bean configuration part
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceXmlLocation" value="classpath*:META-INF/
persistence.xml"/>
<property name="dataSource" ref="..."/>
<property name="jpaVendorAdapter" ref="..."/>
<property name="jpaPropertyMap">
<map merge="true">
<entry key="hibernate.default_batch_fetch_size" value="$
{settings.hibernate.default_batch_fetch_size}"/>
<entry key="hibernate.generate_statistics" value="$
{settings.hibernate.generate_statistics}"/>
<entry key="hibernate.cache.use_structured_entries" value="$
{settings.hibernate.cache.use_structured_entries}"/>
<entry key="hibernate.default_entity_mode" value="$
{settings.hibernate.default_entity_mode}"/>
<entry key="hibernate.use_structured_cache" value="$
{settings.hibernate.use_structured_cache}"/>

<entry key="hibernate.jdbc.batch_size" value="30"/>
<entry key="hibernate.order_inserts" value="true"/>
<entry key="hibernate.order_updates" value="true"/>

<!-- Hazelcast -->
<entry key="hibernate.cache.provider_class"
value="com.hazelcast.hibernate.provider.HazelcastCacheProvider"/>
<entry key="hibernate.cache.use_second_level_cache" value="true"/>
<entry key="hibernate.cache.use_query_cache" value="true"/>
<entry key="hibernate.cache.use_minimal_puts" value="true"/>

<entry key="hibernate.cache.hazelcast.usenativeclient"
value="true" />
<entry key="hibernate.cache.hazelcast.nativeclienthosts"
value="localhost"/>
<entry key="hibernate.cache.hazelcast.nativeclientgroup"
value="dev" />
<entry key="hibernate.cache.hazelcast.nativeclientpassword"
value="dev-pass" />
</map>
</property>
</bean>

Mehmet Dogan

unread,
Aug 24, 2010, 8:40:24 AM8/24/10
to haze...@googlegroups.com
Hi,

I did not get what do you mean exactly here.

> 2. Using the settings as shown above. Only exception is that I create
> a cache manager that puts my objects in the cache
> (myHazelcastClient.getMap(region from the entity-class).put(key,
> object)) after beeing persisted with hibernate.


Does "a cache manager" mean you implemented your own cache manager?
What are key and object that you put into map?

Hibernate does not pure serialized/binary entity-object into cache or does not holds actual instances.
It has its own object dehydration/hydration method to make 2nd level cache more controllable for Hibernate itself.

> --
> You received this message because you are subscribed to the Google Groups "Hazelcast" group.
> To post to this group, send email to haze...@googlegroups.com.
> To unsubscribe from this group, send email to hazelcast+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/hazelcast?hl=en.
>

FooDog

unread,
Aug 24, 2010, 9:19:02 AM8/24/10
to Hazelcast
Thanks for the quick answer.

Yes I implemented my own cache manager or you could say I manually
added the objects to the cache after creating/ updating the objects. I
used the object ID attribute and the object itself. I know this is not
a good way and it's not my preferred way. It was meant to test my
setup.

> Hibernate does not pure serialized/binary entity-object into cache or does not holds actual instances.
> It has its own object dehydration/hydration method to make 2nd level cache more controllable for Hibernate itself.

Shouldn't it not matter how hibernate does the caching? I mean some
data should be in the cache and be available due to the rules I
defined in the hazelcast.xml. I'm not sure how your information can
help me.

Mehmet Dogan

unread,
Aug 24, 2010, 10:11:21 AM8/24/10
to haze...@googlegroups.com
Hmm. If it is about testing Hazelcast cache, it does not matter. Otherwise it means corruption of Hibernate cache.
And yes data should be in cache, and there is a little issue about 2nd level cache using clients (native and super) that I miss.

In your second case; because you touch caching map out of 2nd level cache that problem disappeared.
Data should be in cache due to defined rules.

I do not know exactly if there will be a next 1.9-snapshot or not, but either way, fix will be in 1.9 release.

FooDog

unread,
Aug 24, 2010, 10:22:41 AM8/24/10
to Hazelcast
You are right and I would never use both methods (letting hibernate
cache objects and then write with a cache manager).

How can I see that something has been cached and/ or how much has been
cached by hibernate?

Is the 1.9 release still planed for the end of the month? If so I
would happily wait and test again.

And thanks again for the help.

Mehmet Dogan

unread,
Aug 24, 2010, 11:04:03 AM8/24/10
to haze...@googlegroups.com
To see general numbers, as you already know, you can use hazelcast-monitor and hibernate statistics.

To analyze a little deeper, you can query entity-region map.

for example:
boolean IMap.containsKey(Serializable key)
MapEntry IMap.getMapEntry(Serializable key) => MapEntry contains statistical data about entry.

yes 1.9 is still planned for end of the month. and also there will be new snapshot soon before final release.

Talip Ozturk

unread,
Aug 24, 2010, 11:36:23 AM8/24/10
to haze...@googlegroups.com
The new snapshot, containing the hibernate fix, is ready for download.

twitter @oztalip

Reply all
Reply to author
Forward
0 new messages