Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hazelcast configuration with spring

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 as reference I have configured my sessionFactory as follows:

        <hz:hibernate-region-factory id="regionFactory" instance-ref="instance"
                                 mode="LOCAL" />

    <bean id="sessionFactory"
          class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="packagesToScan" value="com.somePackage" />
        <property name="cacheRegionFactory" ref="regionFactory" />
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.database">ORACLE</prop>
                <prop key="hibernate.show_sql">false</prop>
                <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                <!--enable 2nd level cache-->
                <prop key="hibernate.cache.use_second_level_cache">true</prop>
                <prop key="hibernate.cache.use_query_cache">false</prop>
            </props>
        </property>
    </bean>

Unfortunately, this does not work because I get the following exception:

Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'cacheRegionFactory' of bean class [org.springframework.orm.hibernate4.LocalSessionFactoryBean]: Bean property 'cacheRegionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?

I realize that the documentation example uses Hibernate 3.

What would be the appropriate configuration for Hibernate 4?

This is my hazelcast instance config file:

<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-3.5.xsd">

<context:annotation-config />

<hz:hazelcast id="instance">
    <hz:config>
        <hz:spring-aware />
        <hz:group name="dev" password="password"/>
        <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:map name="default"
                in-memory-format="BINARY"
                backup-count="1"
                async-backup-count="0"
                time-to-live-seconds="0"
                max-idle-seconds="0"
                eviction-policy="NONE"
                max-size="0"
                max-size-policy="PER_NODE"
                eviction-percentage="30"
                min-eviction-check-millis="100"
                merge-policy="com.hazelcast.map.merge.PutIfAbsentMapMergePolicy"/>
    </hz:config>
</hz:hazelcast>

Thank you !


Later edit

Using İbrahim Gürses suggestion seems to fix the undefined property error but a new problem arose:

If I name my hazelcast config file hazelcast.xml I get a java.lang.IllegalStateException: Failed to load ApplicationContext caused by:

Caused by: com.hazelcast.config.InvalidConfigurationException: Your xsd schema couldn't be load

If I give it a different name, for example hazelcast-config.xml I get the same exception, this time caused by:

Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@2cd62003 rejected from java.util.concurrent.ScheduledThreadPoolExecutor@61ab89b0[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 7]

I import (<import resource="" />) my hazelcast config file before the sessionFactory config file.

like image 490
Bob Avatar asked Feb 02 '26 09:02

Bob


1 Answers

The reason you are getting exception is cacheRegionFactory is not defined in org.springframework.orm.hibernate4.LocalSessionFactoryBean. You can do your second level cache configuration without using cacheRegionFactory.

This is your hazelcast instance config file, just giving an instance name to your hazelcast instance with <hz:instance-name>myInstance</hz:instance-name>:

 <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-3.5.xsd">

<context:annotation-config />

<hz:hazelcast id="instance">
    <hz:config>
        <hz:instance-name>myInstance</hz:instance-name>
        <hz:spring-aware />
        <hz:group name="dev" password="password"/>
        <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:map name="default"
                in-memory-format="BINARY"
                backup-count="1"
                async-backup-count="0"
                time-to-live-seconds="0"
                max-idle-seconds="0"
                eviction-policy="NONE"
                max-size="0"
                max-size-policy="PER_NODE"
                eviction-percentage="30"
                min-eviction-check-millis="100"
                merge-policy="com.hazelcast.map.merge.PutIfAbsentMapMergePolicy"/>
    </hz:config>
</hz:hazelcast>

And this is sessionFactory bean last two properties added and HazelcastLocalCacheRegionFactory will use instance defined with name myInstance and you do not need to use <hz:hibernate-region-factory> config in this case.

 <bean id="sessionFactory"
              class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="packagesToScan" value="com.somePackage" />
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.database">ORACLE</prop>
                    <prop key="hibernate.show_sql">false</prop>
                    <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                    <!--enable 2nd level cache-->
                    <prop key="hibernate.cache.use_second_level_cache">true</prop>
                    <prop key="hibernate.cache.use_query_cache">false</prop>
                    <prop key="hibernate.cache.region.factory_class">com.hazelcast.hibernate.HazelcastLocalCacheRegionFactory</prop>
                    <prop key="hibernate.cache.hazelcast.instance_name">myInstance</prop>
                </props>
            </property>
    </bean>
like image 140
ibrahim gürses Avatar answered Feb 03 '26 22:02

ibrahim gürses



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!