【问题标题】:Hazelcast as Hibernate L2 Cache not working (0 hits, misses and puts)Hazelcast 作为休眠 L2 缓存不起作用(0 次命中、未命中和放置)
【发布时间】: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


    【解决方案1】:

    我会确保客户端配置文件被 Hazelcast Hibernate 插件获取。由于您将 hibernate.cache.hazelcast.configuration_file_path 指定为 hazelcast-config.xml 并且您想在 Hibernate 应用程序中使用客户端,因此您的 client 配置文件需要命名为 hazelcast-config.xml 并且它需要位于您的根目录中类路径。

    您还可以通过简单的客户端应用程序检查集群中是否存储了任何数据,或者您可以使用 Hazelcast 管理中心的入口浏览器功能(您可以通过here 下载)。

    在处理 Hibernate 二级缓存统计信息时要注意的另一件事是,查询对缓存统计信息没有任何影响。仅在按 ID 检索时才会增加命中/未命中。因此,如果您的意思是当您说fetching entities 时执行查询,那么命中/未命中的统计信息是0 是正常的。

    【讨论】:

      猜你喜欢
      • 2014-01-21
      • 2020-05-19
      • 2016-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-23
      • 1970-01-01
      相关资源
      最近更新 更多