Hazelcast作为Hibernate L2 Cache无法正常工作(0次点击,未命中和放置)

时间:2018-01-24 06:48:17

标签: hibernate jpa hazelcast second-level-cache

我已在我的计算机上设置了运行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应用程序上,我的hibernate配置如下:

<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 Cache。我觉得0次点击,失误和放置意味着某些事情肯定是错误的,所以我错过了什么?

修改:在版本不匹配的情况下导入FYI 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

1 个答案:

答案 0 :(得分:0)

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

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

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