Hibernate Cache and Spring Integration

758 views
Skip to first unread message

Hari Gangadharan

unread,
Feb 2, 2011, 1:43:25 PM2/2/11
to Hazelcast
Hi All:

For the last few days I am struggling with configuring Hazelcast using
spring integration + Hibernate Second Level Cache. The problem is two
instances of Hazelcast is starting up. I like Hazelcast Spring
integration since we can dynamically configure the parameters based on
the environment I am in. The other case it is not that easy. I
initially thought that both will be same instance but it is not
happening. Hence I started configuring the Hibernate Hazelcast cache
as a native client but JPA properties seems to be ignored by the
Hazelcast Hibernate. Now I am trying with just the Hibernate Second
Level Cache. Unfortunately it is either picking up the hazelcast.xml
or the default configuration XML that is shipped with the jars. I
cannot give a custom configuration file using the property:
hibernate.cache.providerconfigurationfileresourcepath. I have also
used tried hibernate.cache.hazelcast.configurationfilepath and even
the native client configuration properties. Seems like all of these
are ignored by Hazelcast (I am running 1.9.1). Today I will try with
1.9.2 and see if I have any success.

Any one tried Hazelcast new configuration properties in a Spring/JPA/
Hibernate environment?

BTW, Hazelcast 1.9.2 is not there in any maven repositories.

Hari

Mehmet Dogan

unread,
Feb 3, 2011, 3:26:08 AM2/3/11
to haze...@googlegroups.com
Hi,

We have Hazelcast-Hibernate integration, Hazelcast-Spring integration but not both (Hazelcast-Spring-Hibernate integration).
So you are right, when you use both, there will be 2 hazelcast instances. We should improve hazelcast spring configuration to allow create hibernate cache within spring config.
If you file an issue for this request then you can track issue status and we wont forget to add this capability.

Regarding  Hibernate L2 cache configuration; there is a typo in hazelcast.com documentation (for correct one look at: http://code.google.com/docreader/#p=hazelcast&s=hazelcast&t=HibernateSecondLevelCache).

Parameter names are "hibernate.cache.provider_configuration_file_resource_path" and "hibernate.cache.hazelcast.configuration_file_path".


@mm



--
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.


Hari Gangadharan

unread,
Feb 3, 2011, 5:14:52 PM2/3/11
to Hazelcast
Seems like my previous post did not go through. Anyway this is what I
have written:

Thank You! Thank You! Thank You!

Oh! That fixed my issues. Errors in document is worse than having no
document. Can someone take care of correcting the documentation?

First I tried the Hibernate Second Level Cache to be a native client
to the Spring Intergation Instance. That did not work at all. The
Hibernate Second Level Cache started up before the Spring one and it
blew up complaining the cluster is not up. Anyway I now use just the
Hibernate Second Level Cache. The only problem is I will not be able
to use the Hazelcast autowired to a Spring bean unless I start a new
instance. As you have mentioned, we should create a hazelcast-spring-
hibernate integration. Probably we should create a Hibernate Spring
Provider that will use the Spring Hazelcast instance. I will open an
issue to track this.

Hari Gangadharan


On Feb 3, 12:26 am, Mehmet Dogan <mehmetdog...@gmail.com> wrote:
> Hi,
>
> We have Hazelcast-Hibernate integration, Hazelcast-Spring integration but
> not both (Hazelcast-Spring-Hibernate integration).
> So you are right, when you use both, there will be 2 hazelcast instances. We
> should improve hazelcast spring configuration to allow create hibernate
> cache within spring config.
> If you file an issue for this request then you can track issue status and we
> wont forget to add this capability.
>
> Regarding  Hibernate L2 cache configuration; there is a typo in
> hazelcast.com documentation (for correct one look at:http://code.google.com/docreader/#p=hazelcast&s=hazelcast&t=Hibernate...
> ).
>
> Parameter names are "
> hibernate.cache.provider_configuration_file_resource_path" and "
> hibernate.cache.hazelcast.configuration_file_path".
>
> @mm
>
> On Wed, Feb 2, 2011 at 20:43, Hari Gangadharan
> <hari.gangadha...@gmail.com>wrote:
> > hazelcast+...@googlegroups.com<hazelcast%2Bunsubscribe@googlegroups .com>
> > .

Stefan Fiedler

unread,
May 5, 2011, 6:07:12 AM5/5/11
to haze...@googlegroups.com
hi,

regarding the hibernate-spring integration i've found a new constructor in your HazelcastCacheRegionFactory in 1.9.3-RC, so i assume, something like this is the way to use a spring-defined hz instance:

<hz:hazelcast id="hazelcast">...</hz:hazelcast>

<bean id="hazelcastCacheRegionFactory" class="com.sourceheads.util.hazelcast.hibernate.HazelcastCacheRegionFactory">
    <constructor-arg ref="hazelcast"/>
</bean>

<bean id="hibernateSessionFactory"
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="cacheRegionFactory" ref="hazelcastCacheRegionFactory"/>
...
</bean>

at least this works for me ;)

btw: your implementation of the RegionFactory interface is missing a method (i think it came with hibernate 3.5; currently i'm using 3.6.3):

AccessType getDefaultAccessType();

g,
stefan

ps: hz is a great product. keep up the good work! :)

Mehmet Dogan

unread,
May 5, 2011, 6:20:23 AM5/5/11
to haze...@googlegroups.com
Hi,

Yes that works, and also that constructor is exactly for this case. Actually hazelcast spring xml configuration has two additional tags to create CacheProvider and CacheRegionFactory beans.

<hz:hazelcast id="hazelcast">...</hz:hazelcast>
<hz:hibernate-cache-provider id="cacheProvider" instance-ref="instance" />
<hz:hibernate-region-factory id="regionFactory" instance-ref="instance" />

Hazelcast-Hibernate integration is based on Hibernate 3.3.2 and it seems you can not use these because of your Hibernate version. 
I should look at upgraded Hibernate api and fix missing things. Thanks for report..

@mmdogan




Stefan Fiedler

unread,
May 5, 2011, 7:17:38 AM5/5/11
to haze...@googlegroups.com
hello,

yes, that's why i've used my own RegionFactory class implementing that method.

as far as i've tested, everything else from hz's cache implementation seems to work just fine with hibernate 3.6.3.

thx & g,
stefan

Mehmet Dogan

unread,
May 5, 2011, 7:38:09 AM5/5/11
to haze...@googlegroups.com

I have just added methods for Hibernate 3.5.0+, Hazelcast 1.9.3 final build will be compatible both 3.3 and 3.5+ branches.

@mm

Andreas Oskarsson

unread,
May 19, 2011, 12:18:03 PM5/19/11
to Hazelcast
To get this working in my Spring configuration where I configure JPA
rather than Hibernate the following quick hack worked to get one
single Hazelcast instance for the lot, but there must be a better way?

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">

...

<property name="jpaProperties">
<props>
<prop
key="hibernate.cache.region.factory_class">com.dea.util.hazelcast.JpaHazelcastCacheRegionFactory</
prop>
<!-- <prop
key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastCacheRegionFactory</
prop> -->
<prop
key="hibernate.cache.hazelcast.use_super_client">true</prop>
<prop
key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</
prop>
<prop key="hibernate.cache.use_minimal_puts">true</
prop>
<prop key="hibernate.generate_statistics">$
{hibernate.generate_statistics}</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<prop key="hibernate.default_batch_fetch_size">8</
prop>
<prop key="hibernate.jdbc.batch_size">20</prop>
<prop key="hibernate.connection.release_mode">auto</
prop>
</props>
</property>

...

</bean>

public class JpaHazelcastCacheRegionFactory implements RegionFactory {

public JpaHazelcastCacheRegionFactory() {

}

public JpaHazelcastCacheRegionFactory(final Properties properties)
{
this();
}

/**
* {@inheritDoc}
*/
@Override
public CollectionRegion buildCollectionRegion(String regionName,
Properties properties, CacheDataDescription metadata)
throws CacheException {

return JpaHazelcastCacheRegionFactoryDelegate.getInstance()
.buildCollectionRegion(regionName, properties,
metadata);
}

// ...and so on...
}

@Component
public class JpaHazelcastCacheRegionFactoryDelegate implements
RegionFactory {

@Resource
private HazelcastCacheRegionFactory hazelcastRegionFactory;

private static JpaHazelcastCacheRegionFactoryDelegate instance;

private JpaHazelcastCacheRegionFactoryDelegate() {
instance = this;
}

public static JpaHazelcastCacheRegionFactoryDelegate getInstance()
{
return instance;
}

/**
* {@inheritDoc}
*/
@Override
public CollectionRegion buildCollectionRegion(String regionName,
Properties properties, CacheDataDescription metadata)
throws CacheException {

return
this.hazelcastRegionFactory.buildCollectionRegion(regionName,
properties, metadata);
}

// ...and so on...
}

The injected "hazelcastRegionFactory" Spring bean is configured by
Spring like this:

<hz:hazelcast id="hazelcast">

...

</hz:hazelcast>

<hz:hibernate-region-factory id="hazelcastRegionFactory" instance-
ref="hazelcast"/>

Br
Andreas

Andreas Oskarsson

unread,
May 19, 2011, 12:29:53 PM5/19/11
to Hazelcast
<hz:hazelcast id="hazelcast">

...

</hz:hazelcast>

<hz:hibernate-region-factory id="hazelcastRegionFactory" instance-
ref="hazelcast"/>

Br
Andreas

On May 5, 1:38 pm, Mehmet Dogan <meh...@hazelcast.com> wrote:

Andreas Oskarsson

unread,
May 20, 2011, 2:12:49 AM5/20/11
to Hazelcast
An improved version of the workaround:

@Component
public class JpaHazelcastCacheRegionFactory implements RegionFactory {

private static HazelcastCacheRegionFactory
hazelcastCacheRegionFactory;

public HazelcastCacheRegionFactoryWrapper() {

}

public HazelcastCacheRegionFactoryWrapper(final Properties
properties) {
this();
}

@Autowired(required = true)
public void setHazelcastCacheRegionFactory(
HazelcastCacheRegionFactory hazelcastCacheRegionFactory) {
HazelcastCacheRegionFactoryWrapper.hazelcastCacheRegionFactory
= hazelcastCacheRegionFactory;
}

@Override
public CollectionRegion buildCollectionRegion(String regionName,
Properties properties, CacheDataDescription metadata)
throws CacheException {

return
HazelcastCacheRegionFactoryWrapper.hazelcastCacheRegionFactory
.buildCollectionRegion(regionName, properties,
metadata);
}

...
}

Br
Andreas

Mehmet Dogan

unread,
May 20, 2011, 2:40:19 AM5/20/11
to haze...@googlegroups.com
If you want to use JPA style, you should probably use some workarounds. Do you use hazelcast separately, out of JPA?

If you configure hazelcast with 'hazelcast.xml' (not in spring), and in spring configuration create hazelcast instance via factory method Hazelcast.getDefaultInstance() then HazelcastCacheRegionFactory will use that default instance. But as I see your instance is super-client, so this approach is not applicable for your case.

@mmdogan

Andreas Oskarsson

unread,
May 20, 2011, 7:38:52 AM5/20/11
to Hazelcast
Well my idea is to use Hazelcast both as the clustered 2nd level cache
for Hibernate (which we obviously are using underneath JPA), as well
as a distributed clustered cache for these same nodes that would hold
whatever relevant shared data for this cluster. What would be your
suggested approach to this then?

1) Two separate Hazelcast configurations/instances?

- Do the Hibernate 2nd level cache config in hazelcast.xml (non
Spring)
- Do the "custom" stuff via Spring and use a separate cluster
group than for the Hibernate stuff

2) One shared Hazelcast configuration/instance?

- Do everything via Spring configuration (using some workaround)

By the way, whether or not to use a super-client in these different
setups is also something that I've not quite certain about...

Br,
Andreas

On May 20, 8:40 am, Mehmet Dogan <meh...@hazelcast.com> wrote:
> If you want to use JPA style, you should probably use some workarounds. Do
> you use hazelcast separately, out of JPA?
>
> If you configure hazelcast with 'hazelcast.xml' (not in spring), and in
> spring configuration create hazelcast instance via factory method
> Hazelcast.getDefaultInstance() then HazelcastCacheRegionFactory will use
> that default instance. But as I see your instance is super-client, so this
> approach is not applicable for your case.
>
> @mmdogan
>
> On Fri, May 20, 2011 at 9:12 AM, Andreas Oskarsson
> <dea.oskars...@gmail.com>wrote:

Mehmet Dogan

unread,
May 20, 2011, 11:05:22 AM5/20/11
to haze...@googlegroups.com
Using two separate instances would be over-use of resources. Workaround is just fine. There is no need to use a super-client unless you don't want to store data on that node.

@mmdogan

Navjot Singh

unread,
May 20, 2011, 1:56:07 PM5/20/11
to haze...@googlegroups.com
Hi Andreas,

Having hibernate instance and other instance in different groups can get you
a clean separation of data -- data which is used solely by hibernate and
data which other business logic components may put and share among
themselves.

However, you will notice that is hibernate and app instance are runinng
within same jvm, your memory constraints are still limited by the same
overall bound.

So, my opinion would be that if you have a clear requirement to maintain a
separate cache cluster for data shared amoung business services which
non-native clients should access, sizing constraints of which should not be
affected by l2 cache regions of hibernate, you may keep a single hzinstance
per jvm.

You may not even need to use any of HZ's spring support for this.

a. keep your config in xml -- may be you have diff config for diff env like
qa, prods etc.
b. create a hz instance using a simple class which takes your config.
c. Use the same instance as constructor-arg to hazelcastcacheregionfactory
d. Inject this cacheregionfactory instance to Hibernate's SessionFactoryBean
e. Use the hzinstance in your app code.

However, there are other ways for this workaround as well, as listed in this
very thread.

my 2 cents
Navjot Singh

Andreas Oskarsson

unread,
May 21, 2011, 2:50:21 PM5/21/11
to Hazelcast
Thanks for input!

I now have a stable application with a single Hazelcast instance for
both Hibernate + custom stuff running without any workarounds:

<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
...
<property name="jpaProperties">
<prop
key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastCacheRegionFactory</
prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
...
</properties>
...
</bean>

The Hazelcast configuration is done in a hazelcast.xml file instead of
via Spring, but since an instance of Hazelcast will get created behind
the scenes then the following Spring configuration gets me a reference
to this instance for custom use:

<bean id="hazelcast" class="com.hazelcast.core.Hazelcast" factory-
method="getDefaultInstance" />

...

@Service
public class MyService {

@Resource
private HazelcastInstance hazelcast;

...
}

// Andreas
Reply all
Reply to author
Forward
0 new messages