【发布时间】:2018-07-03 03:19:47
【问题描述】:
我已经在我的机器上设置了一个正在运行的 Hazelcast 服务器实例,配置如下:
<hz:hazelcast id="hazelcast_server">
<hz:config>
<hz:group name="dev" password="dev" />
<hz:properties>
<hz:property name="hazelcast.merge.first.run.delay.seconds">5</hz:property>
<hz:property name="hazelcast.merge.next.run.delay.seconds">5</hz:property>
</hz:properties>
<hz:network port="4031" port-auto-increment="false">
<hz:join>
<hz:multicast enabled="true" />
</hz:join>
</hz:network>
</hz:config>
</hz:hazelcast>
在另一个 JVM 应用程序上,我的休眠配置如下:
<util:map id="hibernateConfig">
<entry key="hibernate.default_schema" value="" />
<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<entry key="hibernate.hbm2ddl.auto" value="none" />
<entry key="hibernate.format_sql" value="false" />
<entry key="hibernate.show_sql" value="true" />
<entry key="hibernate.check_nullability" value="false" />
<entry key="hibernate.archive.autodetection" value="class" />
<entry key="hibernate.listeners.envers.autoRegister" value="true" />
<entry key="hibernate.ejb.metamodel.population" value="disabled" />
<entry key="hibernate.search.default.directory_provider" value="filesystem" />
<entry key="hibernate.search.default.indexBase" value="lucene\indexes" />
<entry key="org.hibernate.flushMode" value="COMMIT" />
<!-- Enable L2 cache. -->
<entry key="cache.use_second_level_cache" value="true" />
<entry key="generate_statistics" value="true" />
<entry key="hibernate.cache.region_prefix" value="myApp"/>
<entry key="hibernate.cache.region.factory_class" value="com.hazelcast.hibernate.HazelcastCacheRegionFactory" />
<entry key="hibernate.cache.hazelcast.configuration_file_path" value = "hazelcast-config.xml"/>
<entry key="hibernate.cache.hazelcast.use_native_client" value="true" />
</util:map>
我的可缓存实体是:
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class Simple {
@Id @GeneratedValue
int id;
String name;
我的 Hazelcast 客户端实例配置如下:
<group>
<name>dev</name>
<password>dev</password>
</group>
<instance-name>hazelcast_client</instance-name>
<network>
<discovery-strategies>
<discovery-strategy
class="com.hazelcast.spi.discovery.multicast.MulticastDiscoveryStrategy"
enabled="true">
<properties>
<property name="group">localhost</property>
<property name="port">4031</property>
</properties>
</discovery-strategy>
</discovery-strategies>
<cluster-members>
<address>localhost:4031</address>
</cluster-members>
<connection-attempt-limit>3</connection-attempt-limit>
<socket-options>
<reuse-address>true</reuse-address>
</socket-options>
</network>
在将 2 个实体持久化到本地托管的 MySQL 数据库 - 结束事务 - 获取实体 - 结束事务后,我尝试使用以下方法获取统计信息:
for(String s :sessionFactory.getStatistics().getSecondLevelCacheRegionNames())
{
System.out.println("[H-STATS] For region: \n"+ s + ":{"
+ "\n\tHit count: "+sessionFactory.getStatistics().getSecondLevelCacheStatistics(s).getHitCount()
+ "\n\tMiss count: "+sessionFactory.getStatistics().getSecondLevelCacheStatistics(s).getMissCount()
+ "\n\tPut count: "+sessionFactory.getStatistics().getSecondLevelCacheStatistics(s).getPutCount()
+ "\n\tElement Count on disk: "+sessionFactory.getStatistics().getSecondLevelCacheStatistics(s).getElementCountOnDisk()
+ "\n\tElement Count in memory: "+sessionFactory.getStatistics().getSecondLevelCacheStatistics(s).getElementCountInMemory()
+ "\n\tSize in memory: "+sessionFactory.getStatistics().getSecondLevelCacheStatistics(s).getSizeInMemory()
+"\n}");
}
但我得到的是:
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: select next_val as id_val from hibernate_sequence for update
Hibernate: update hibernate_sequence set next_val= ? where next_val=?
Hibernate: insert into Simple (name, id) values (?, ?)
Hibernate: insert into Simple (name, id) values (?, ?)
Jan 24, 2018 12:09:30 PM org.hibernate.hql.internal.QueryTranslatorFactoryInitiator initiateService
INFO: HHH000397: Using ASTQueryTranslatorFactory
Hibernate: select simple0_.id as id1_0_, simple0_.name as name2_0_ from Simple simple0_
[com.myapp.entities.Simple@1707ef71, ... com.myapp.entities.Simple@5bea4da2]
[H-STATS] For region:
myApp.com.myapp.entities.Simple:{
Hit count: 0
Miss count: 0
Put count: 0
Element Count on disk: -1
Element Count in memory: 28
Size in memory: 11924
}
目前我的主要目标是检查我是否已正确地将 hazelcast 配置为 Hibernate L2 缓存。我觉得获得 0 次命中、未命中和未命中意味着肯定有问题,那么我错过了什么?
编辑:在版本不匹配的情况下,仅供参考 Maven 导入:
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast</artifactId>
<version>3.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.hazelcast/hazelcast-client -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-client</artifactId>
<version>3.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.hazelcast/hazelcast-spring -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-spring</artifactId>
<version>3.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.hazelcast/hazelcast-hibernate52 -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-hibernate52</artifactId>
<version>1.2.2</version>
</dependency>
我使用的是 Hibernate 5.2.8.final
【问题讨论】:
标签: hibernate jpa hazelcast second-level-cache