Hazelcast configuration for Hibernate 2nd level cache and Spring Cache

668 views
Skip to first unread message

Bogdan Pistol

unread,
Feb 4, 2016, 6:19:38 AM2/4/16
to Hazelcast

I am trying to configure Hazelcast 3.5.4 to work as a 2nd level cache for Hibernate 4.2.8 and as Spring 3.1.2 Cache.

I have added the dependecies:


    <dependency>

            <groupId>com.hazelcast</groupId>

            <artifactId>hazelcast-hibernate4</artifactId>

            <version>${hazelcast-version}</version>

        </dependency>


        <dependency>

            <groupId>com.hazelcast</groupId>

            <artifactId>hazelcast</artifactId>

            <version>${hazelcast-version}</version>

        </dependency>


        <dependency>

            <groupId>com.hazelcast</groupId>

            <artifactId>hazelcast-spring</artifactId>

            <version>${hazelcast-version}</version>

        </dependency>


Using the documentation and the sample application I have the following configuration.


My session factory:

        <bean id="sessionFactory"

              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton">

            <property name="dataSource" ref="dataSource" />

            <property name="packagesToScan" value="com.myPackage" />

            <property name="hibernateProperties">

                <props>

                    <prop key="hibernate.database">ORACLE</prop>

                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

                      <prop key="hibernate.cache.use_second_level_cache">true</prop>

                    <prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory</prop>

                    <prop key=" hibernate.cache.hazelcast.instance_name">myInstance</prop>

                    <prop key="hibernate.cache.use_query_cache">false</prop>

                </props>

            </property>

        </bean>


My hazelcast config file using hazelcast namespace: hazelcast-config.xml


<beans xmlns="http://www.springframework.org/schema/beans"

       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

       xmlns:context="http://www.springframework.org/schema/context"

       xmlns:hz="http://www.hazelcast.com/schema/spring"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd

                http://www.springframework.org/schema/context

                http://www.springframework.org/schema/context/spring-context-3.0.xsd

                http://www.hazelcast.com/schema/spring

                http://www.hazelcast.com/schema/spring/hazelcast-spring.xsd">


    <context:annotation-config />

    <hz:hazelcast id="instance">

        <hz:config>

            <hz:instance-name>myInstance</hz:instance-name>

            <hz:group name="dev" password="pass"/>

            <hz:network port="5701" port-auto-increment="true">

                <hz:join>

                    <hz:multicast enabled="true"

                                  multicast-group="224.2.2.3"

                                  multicast-port="54327"/>

                </hz:join>

            </hz:network>

        </hz:config>

    </hz:hazelcast>

</beans>


The issue is that I get a Failed to load ApplicationContext exception.


Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'instance': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public static com.hazelcast.core.HazelcastInstance com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.config.Config)] threw exception; nested exception is java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@16ac4d3d rejected from java.util.concurrent.ScheduledThreadPoolExecutor@559d19c[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 8] 


The problem seems to be that hazelcast is reading hazelcast-default.xml and starting an instance before reading my configuration.

I want to have the spring cache and hibernate cache both on the same instance.


Any help is appreciated!

Thank you !


İbrahim Gürses

unread,
Feb 4, 2016, 7:53:30 AM2/4/16
to Hazelcast
Hi Bogdan, 
There is an extra space in the beginning of your your configuration of hibernate instance name property :

   <prop key=" hibernate.cache.hazelcast.instance_name">myInstance</prop>  should be    <prop key="hibernate.cache.hazelcast.instance_name">myInstance</prop>

Also I  modified code sample for hibernate 3 in my own fork in order to work with with hibernate 4. Hibernate Second Level Cache is using the same instance defined with spring.

These are my spring and hibernate versions that  I did testing :

 <spring.version>3.1.2.RELEASE</spring.version>
 <hibernate.version>4.2.8-Final</hibernate.version>

Modified code-sample is working. If you have any other problems, please let me know.

Best
İbrahim.

Bogdan Pistol

unread,
Feb 5, 2016, 4:04:01 AM2/5/16
to Hazelcast

Hello Ibrahim,


 Thank you for helping me out. After I removed the extra space, everything seems to be working!  I can't believe I missed that...

The only small issue seems to be the order in which I import the hazelcast config file. If it is not among the first to be imported I get an error:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'instance': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public static com.hazelcast.core.HazelcastInstance com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.config.Config)] threw exception; nested exception is com.hazelcast.core.DuplicateInstanceNameException: HazelcastInstance with name 'myInstance' already exists!

For example, this causes an error:

    <context:component-scan base-package="com.myPackage.*" annotation-config="true"/>
    <import resource="classpath:hazelcast-config.xml" />

while importing hazelcast before component scan works fine.

İbrahim Gürses

unread,
Feb 5, 2016, 11:01:53 AM2/5/16
to Hazelcast
Hi Bogdan,

The ordering causes problem, you are right.  What happening in the background is, when you import hazelcast-config.xml after the component scan, firstly instance for hibernate instance is created because of component scan and after that you import hazelcast config and an hazelcast instance is being created with instance-name. But apparently hazelcast-spring configuration does not use the instance already created and tries to create the new instance with your instance-name and that is why you get DuplicateInstanceNameException. Spring instance creation does not work the way the hibernate does.

I do not know, this is a design choice or bug. But you can create an issue on git-hub for this, seems a valid use case for me.(https://github.com/hazelcast/hazelcast/issues/new).

Right know, the only way for using the same instance for both hibernate and spring, you need to make sure hibernate instance created after the spring configured hazelcast instance.

Thanks for your findings.

Best.
Ibrahim

Bogdan Pistol

unread,
Feb 24, 2016, 7:01:59 AM2/24/16
to Hazelcast
A possible solution to the ordering issue can be to add depends-on to the sessionFactory bean.

<bean id="sessionFactory"

              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean" scope="singleton" depends-on= "hazelcast-config.xml" >

Reply all
Reply to author
Forward
0 new messages