我正在尝试将Hazelcast(3.5.4)配置为Hibernate(4.2.8)和Spring(3.1.2)Cache的二级缓存。
我添加了依赖:
<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>
使用documentation作为参考我已按如下方式配置了sessionFactory:
<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>
不幸的是,这不起作用,因为我得到以下异常:
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?
我意识到文档示例使用了Hibernate 3。
Hibernate 4的适当配置是什么?
这是我的hazelcast实例配置文件:
<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>
谢谢!
稍后编辑
使用İbrahimGürses建议似乎修复了未定义的属性错误,但出现了一个新问题:
如果我将hazelcast配置文件命名为hazelcast.xml,则会导致java.lang.IllegalStateException: Failed to load ApplicationContext
导致:
Caused by: com.hazelcast.config.InvalidConfigurationException: Your xsd schema couldn't be load
如果我给它一个不同的名称,例如hazelcast-config.xml,我会得到相同的异常,这次由以下原因引起:
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]
我在sessionFactory配置文件之前导入(<import resource="" />
)我的hazelcast配置文件。
答案 0 :(得分:1)
您获得异常的原因是cacheRegionFactory
中未定义org.springframework.orm.hibernate4.LocalSessionFactoryBean
。您可以在不使用cacheRegionFactory
的情况下执行二级缓存配置。
这是您的hazelcast实例配置文件,只是使用<hz:instance-name>myInstance</hz:instance-name>
为您的hazelcast实例提供实例名称:
<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>
这是sessionFactory
bean最后添加的两个属性,HazelcastLocalCacheRegionFactory
将使用名称为myInstance
的实例定义,在这种情况下您不需要使用<hz:hibernate-region-factory>
配置。
<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>